Getting an error in Gdscript setting up character 2D movment

Godot Version

Replace this line with your Godot version

Question

I am learning Godot via gamedev.tv course and got this course they offer “Godot 4 Multiplayer: Make Your Own Online Game” I copied the code the way the instructor entered it. I have gone over it a few times comparing it and I don’t see the issue. When I search google it said I need to declare my varibles at the top of the code as a Varbile which it is “var movement_speed = 300.0” The help docs on Godots site is unclear to an amature like me. I know if your a programmer it probabley makes sense but when your new it didn’t make any sense. So I am not understanding how to fix it.

Thanks in advance for the help

(It said I needed to wrap my code inside of three backticks I dont know what a back ticks are)

extends CharacterBody2D

@export var movement_speed = 300.0
@export var gravity = 30.0
@export var jump_strength = 600.0

func _physics_process(_delta : float):
var horizontal_input = (
Input.get_action_strength(“move_right”)
-Input.get_action_strength(“move_left”)
)
#Godot velocity .x for horizon and .y for verticle force being appplied
velocity.x = horizontal_input * movement_speed
velocity.y += gravity

if Input.is_action_just_pressed("jump") and is_on_floor():
	#Godot up is a - value thats why jump is -jump_strength
	velocity.y = -jump_strength
move_and_slide()

This is my error

E 0:00:01:321   _physics_process: Invalid operands 'float' and 'Nil' in operator '*'.
  <GDScript Source>player.gd:13 @ _physics_process()
  <Stack Trace> player.gd:13 @ _physics_process()

Back ticks are the reverse single quote-like character found on the ~ key.
You can also just use the </> on the tool bar of the message box.
You can also go back and edit your post to fix the code formatting.

The error is telling you that either horizontal_input or movement_speed is null (it hasn’t been defined)
Since movement_speed has a default value I am guessing that horizontal_input is the problem.
horizontal_input is defined on the line prior to the error

var horizontal_input = (Input.get_action_strength(“move_right”)-Input.get_action_strength(“move_left”))

First test to see if horizontal_input results in anything:

var horizontal_input = (Input.get_action_strength(“move_right”)-Input.get_action_strength(“move_left”))
print(horizontal_input)

If that prints null then you know it is the problem.
If you give the variable a type you may get a much better error message:

var horizontal_input:float = (Input.get_action_strength(“move_right”)-Input.get_action_strength(“move_left”))

There is no need to enclose the math in (). It makes me wonder if the tutorial did that or is your code not quite exactly the same as the tutorial:

var horizontal_input:float = Input.get_action_strength(“move_right”)-Input.get_action_strength(“move_left”)

It looks to me like you have not set up the input mappings for move_left and move_right.
In that tutorial he should have shown you how to set those mappings.
Under Project/ProjectSettings/InputMap select add new action, type in move_left and hit the + Add .
Then select the newly listed action and hit the big plus sign to the right of it. Then hit whatever key or input device you use to move left and it should register it.
Repeat for move_right.

I’ve tried your code and got no issues. You may need to decrease movement_speed and jump_strength, but the logic works so far

Your error says you’re trying to multiply float and nil so please make sure movement_speed is a number

Yeah the learning curve is a bit steep especially if you have no experience or are new to it, but once you get it you can create amazing games with it!

Backtick is the button under the ESC, above Tab. So when you wanna insert your code, wrap it like this

‎ ```gdscript
‎ Your code here
‎ ```

Feel free to ask any further questions!