Player:Move called, but player currently has no character

This is my script and it keeps telling me “Player:Move called, but player currently has no character.” but idk how to fix it.
Script:


local character = script.Parent

if character then
	task.wait()
	local humanoid = character:WaitForChild("Humanoid") 


local walkDirection = Vector3.new(0, 0, 1)
local walkSpeed = 16 

while true do
	local destination = character.PrimaryPart.Position + walkDirection * walkSpeed 
	humanoid:MoveTo(destination) -- Use Humanoid:MoveTo() to move the character to the destination
	local characterForward = character.PrimaryPart.CFrame.LookVector
	characterForward = Vector3.new(characterForward.X, 0, characterForward.Z).Unit -- Zero out the Y component to keep the character upright
	character.PrimaryPart.CFrame = CFrame.new(character.PrimaryPart.Position, character.PrimaryPart.Position + characterForward)
	task.wait() -- Wait for one frame for smooth movement
	end
end

it probably means that the character hasn’t loaded yet but I also tried waiting for it but nothing worked.

Is this in StarterCharacterScripts?

yes it is in StarterCharacterScripts)

Try putting a
game.Players.LocalPlayer.CharacterAdded:Wait()
or a
repeat wait() until game.Players.LocalPlayer.Character ~= nil
at the very start of the script. I’m not sure whether or not this will work but it will be a good attempt though.

How can you confirm Player:Move is called when there is no call to that function in the code you posted?

This continuous loop will most likely create undesired behaviour (especially if the distance to be moved can’t be covered in a single frame):

while true do
	local destination = character.PrimaryPart.Position + walkDirection * walkSpeed 
	humanoid:MoveTo(destination)

The lookVector manipulation below is redundant if you choose the location the humanoid is walking to with greater care:

	local characterForward = character.PrimaryPart.CFrame.LookVector
	characterForward = Vector3.new(characterForward.X, 0, characterForward.Z).Unit -- Zero out the Y component to keep the character upright
	character.PrimaryPart.CFrame = CFrame.new(character.PrimaryPart.Position, character.PrimaryPart.Position + characterForward)