Problem with 2d arms and detecting player movement

I have a script that should alternate between the visibility of two guis when walking, and enable the visibility of one when standing still, to make a 2d animation of walking. However, the method im using to detect player movement can cause the guis to flicker rapidly whenever there is a change in movement magnitude. how can i make the script work so that the function is connected whenever the character is moving instead of whenever the players magnitude changes? Everything else in the script works except for this.

my code (in a localscript under starterplayerscripts)

local plr = game:GetService("Players").LocalPlayer
local player = plr.Character or plr.CharacterAdded:Wait()
local hum = player:WaitForChild("Humanoid")
local gui = plr.PlayerGui
local neu = gui.gamescreen.armsidle
local one = gui.gamescreen.armsmove1
local two = gui.gamescreen.armsmove2
local armscheck = 0
local magncheck = 0

print("check")

hum:GetPropertyChangedSignal("MoveDirection"):Connect(function()

	if hum.MoveDirection.Magnitude > 0 then

		magncheck = 1

		while magncheck == 1 do

			if armscheck == 0 then

				one.Visible = true
				neu.Visible = false
				two.Visible = false

				armscheck = 1

			elseif armscheck == 1 then

				one.Visible = false
				neu.Visible = false
				two.Visible = true

				armscheck = 0

			else
				return
			end

			wait(0.2)

		end

	end

	if hum.MoveDirection.Magnitude == 0 then

		magncheck = 0

	end

end)

hum:GetPropertyChangedSignal("MoveDirection"):Connect(function()

	one.Visible = false
	neu.Visible = true
	two.Visible = false

end)
1 Like

Why have you binded movement to MoveDirection instead of the movement keys? It’d be more logical to play the animation when wasd is pressed surely?

With the movement keys, there would be problems for playability on console and mobile, although i guess i could just trigger the event for every possible input on every device.

The property would fire too often if you binded it to MoveDirection, just inefficient and will stutter

Put the animation code in a local function and call upon it whenever a movement input is detected