Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

i got error attempt to compare Vector3 < Vector3?

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

3 comments

0

.MovedDirection returns a vector3

0

try using CFrame

0

jeremqmfpi can you check my answer. if it helped you, please accept it!

2 answers

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.

##Problem:

The reason it didn't work was that you cannot compare a vector3 datatype to another vector3.

##Solution:

Make sure to add a magnitude, as that can get the distance of the humanoid's MoveDirection and make it work!

##Fixed Script:



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!

This discussion is preserved as it was. New answers, comments, and votes are closed.