local Players = game:GetService("Players") local rp = game.ReplicatedStorage local summon = rp:WaitForChild("Summon") local Ismoving = true print("1") summon.OnServerEvent:Connect(function(Player, isActive) print("2") local character = Player.Character local Stand = character:WaitForChild("Stand") local AnimControl = Stand:FindFirstChild("AnimControl") local humanoid = character:WaitForChild("Humanoid") print("4") local Idle = AnimControl:LoadAnimation(script.Parent.Idle) local MovingAnim = AnimControl:LoadAnimation(script.Parent.MovingAnim) local Stand = character:WaitForChild("Stand") if humanoid.MoveDirection.Magnitude > 0 then Ismoving = true else Ismoving = false end wait(0.1) if Ismoving == true then MovingAnim:Play() print("5") else Idle:Play() print("5") end end) print("Done")
How do I always check the magnitude without putting it in a loop
:GetPropertyChangedSignal
is a function that returns you an event, this event triggers every time specified property changes, you can :Connect
a function to this event which will get executed every time it triggers, for your case the property is MoveDirection
.
humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function() Ismoving = humanoid.MoveDirection.Magnitude > 0 end
If you want it stop from executing the function, you need to disconnect the connection created with :Connect
, this is done using :Disconnect
function which is located inside of connection object returned by :Connect
:
local connection = humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function() Ismoving = humanoid.MoveDirection.Direction > 0 end -- Will check for MoveDirection changes for 5 seconds, then it will disconnect task.wait(5) connection:Disconnect()
Do you mean something like this?
humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function() if humanoid.MoveDirection.Magnitude > 0 then if Ismoving then return end Ismoving = true MovingAnim:Play() else Ismoving = false MovingAnim:Stop() end end