move_and_slide is a function in the KinematicBody/KinematicBody2D node, but your script says you are extending a Node2D node, which does not have this function. This code works in the Godot script editor because the KinematicBody2D node extends the Node2D class, however this script will be unable to access anything beyond the functions defined in the Node2D class.
To use functions defined in the KinematicBody2D node, just replace extends Node2D with extends KinematicBody2D. This will tell Godot that the script should access the functions within the KinematicBody2D class. Assuming the node this script is attached to is a KinematicBody2D node, everything should work :smile:
(Side note: welcome to the forums!)