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

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


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

✓ Accepted answer

: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

4 comments

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 )

0

ya mean this?

0

direction is not a valid member of vector 3

0

pardon cat i meant .Magnitude not .Direction ;w:

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