Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

i got error attempt to compare Vector3 < Vector3?

Asked by 2 years ago

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
0
.MovedDirection returns a vector3 JesseSong 3916 — 2y
0
try using CFrame Cyrus_O4 45 — 2y
0
jeremqmfpi can you check my answer. if it helped you, please accept it! JesseSong 3916 — 2y

2 answers

Log in to vote
3
Answered by
appxritixn 2235 Moderation Voter Community Moderator
2 years ago

Please provide code with your answers. Simply posting an explanation does not help someone new to programming understand how to implement a concept programatically.

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.

Ad
Log in to vote
0
Answered by
JesseSong 3916 Moderation Voter Community Moderator
2 years ago
Edited 2 years ago

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!

Answer this question