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

How do I always check the players magnitude without putting it in a loop?

Asked by 2 years ago

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

1 answer

Log in to vote
0
Answered by
imKirda 4491 Moderation Voter Community Moderator
2 years ago
Edited 2 years ago

: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
0
it detects the slightest change and resets the animation but what I want it to do is just check if the player is moving or not ( because when it detects a change in the magnitude it resets the animation ) Agent0fChaos0 14 — 2y
0
ya mean this? imKirda 4491 — 2y
0
direction is not a valid member of vector 3 Agent0fChaos0 14 — 2y
0
pardon cat i meant .Magnitude not .Direction ;w: imKirda 4491 — 2y
Ad

Answer this question