i am having a lot of trouble with making my custom character walk
error is at line 17
local player = script.Parent local humanoid = player.Humanoid local walk = script:WaitForChild("walk") local walk_anim = humanoid.Animator:LoadAnimation(script.walk) local walking_value = script.walking.Value local player = game.Players.LocalPlayer local speed = humanoid.MoveDirection --walk_anim:Play() function moved() --walk_anim:Play() --print("played animation") if speed == Vector3.new(0,0,0) then walk_anim:Stop() end if speed > Vector3.new(0,0,0) then print("player moved") walk_anim:Play() end end while true do humanoid.Changed:Connect(moved) print("fired function") wait(0.1) end
It seems that you can’t compare Vector3’s using <=, <, >, or >= because Roblox doesn’t implement __lt nor __le metamethods.
A vector of 0, 0, 0 (X,Y,Z) always has a magnitude of 0, so you can compare the magnitude of a vector with another vector's magnitude.
The reason it didn't work was that you cannot compare a vector3 datatype to another vector3.
Make sure to add a magnitude, as that can get the distance of the humanoid's MoveDirection and make it work!
local player = script.Parent local humanoid = player:WaitForChild("Humanoid") local walk = script:WaitForChild("walk") local walk_anim = humanoid.Animator:LoadAnimation(script.walk) local walking_value = script.walking.Value --walk_anim:Play() function moved() --walk_anim:Play() --print("played animation") if humanoid.MoveDirection == Vector3.new(0,0,0) then walk_anim:Stop() end print("test") if humanoid.MoveDirection.Magnitude > 0 then -- change to your preference print("player moved") walk_anim:Play() end end while true do humanoid.Changed:Connect(moved) print("fired function") wait(5) -- added wait, to decrease lag! end
P.S: This took me a whole day to test, edit and debug so an upvote and an accepted answer would help me out. Thank you!