How to play a animation when the player is moving without using Keycodes?

Hey so I have a int value in a folder that increases or decreases dependent on if the player is walking or not.

This is probably a face palm type post but for some reason I’m getting the error described in the title. My smooth brain cant seem to find the issue.

I’m using the value so that the server script in the model can tell if the player is walking that way if the player is walking the idle animation will be disabled (I have animations being controlled between both the server script and local script)

local WalkingValue = ValueFolder:WaitForChild("Walking")

UserInputServ.InputBegan:Connect(function(input, gameProcessed)
	
	if input.KeyCode == Enum.KeyCode.W then
		
		WalkingAnim.Looped = true
		WalkingAnim:Play()
		
		WalkingValue.Value = 1
		
	end
	
-- yes I have all the ends and stuff just alot goes on in this part of the script so im not pasting all the other keycodes --

Everything else works fine but the value changing

1 Like

First of all, I wouldn’t recommend using what you are for detecting if the player is moving. What if they press A, S, or D? What if they’re on mobile or console? (those two are unimportant if your game is for PC only).
You should instead use a GetPropertyChangedSignal event with the humanoid’s MoveDirection property. Then you should check if the magnitude of their MoveDirection is greater than zero (i.e. if they are moving).

Second of all, if this is a LocalScript (which I’m assuming it is since you’re using UserInputService), or if the script’s run context is set to Client, then the server script will have no idea the value even changed. You should use a remote event to change the value

You should use this code instead
local Players = game:GetService("Players")

local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
	if humanoid.MoveDirection.Magnitude > 0 then
		WalkingAnim.Looped = true
		WalkingAnim:Play()
	end
end)

It will also completely eliminate the need for the WalkingValue. You just need to recrate the same GetPropertyChangedSignal event in the server script, like this:

Server Code
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")

humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
	if humanoid.MoveDirection.Magnitude == 1 then
		--// run your code here
	end
end)
3 Likes

an Animation plays when the player presses W or A or S or D
the animations are make the players arms move left is the player is moving right and the opposite of that
and yeah the game is pc only
Thanks for the actual solution on how to do this btw its greatly appreciated :smiley:

1 Like

Hmm still doesn’t work. There could be a bunch of different stuff interfering with it causing issues. Thank you for the help but with my scripts it aint working. I’ll just remove the problematic part of the idle animation. I hope you have a good day and or night. This seems like a good thing to know so thanks again :slight_smile:

1 Like

You need to set up remote events for all the LocalScripts that need to communicate with the server. I would also put the folder that holds the int value into StarterPlayer.

1 Like

Hey sorry to bring you back to this but is there a way I could add a wait into the code you wrote?
I’m trying to use it but it just Repeats the code a million times like when you forget to add a wait to a while true do
The animation is 1 second long
I tried doing .Stopped waits and normal waits but cant seem to figure it out

Switch out the GetPropertyChangedSignal event with this new one
WalkingAnim.Looped = true

humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
	if humanoid.MoveDirection.Magnitude > 0 then
		if not WalkingAnim.IsPlaying then
			WalkingAnim:Play()
		end
	else
		if WalkingAnim.IsPlaying then
			WalkingAnim:Stop()
		end
	end
end)

All I did was add an if statement to check if WalkingAnim.IsPlaying is false. .IsPlaying returns a boolean on if the track is actively playing or not.
I also added an else statement in the .Magnitude check, so it will now stop the animation (if it is playing) and the player is not moving.

I also moved where WalkingAnim.Looped gets set. There is no need to set it to true every single time it runs.

1 Like

Can you send a screenshot of the folder that has the intvalue object? And you’re certain the error is in the code you provided?

1 Like

Amora has found a good solution for it and my old ways were messy anyways! (I should probably close this in a min) Thank you for contributing though :smiley:

Hi. That’s great to hear! People would benefit from an explanation as to how it was resolved.

Well Its solved by replacing pretty much my entire old system which wasn’t even shown in the initial issue and the original question isn’t exactly answered however the problem is solved
so I’m not exactly sure if it would be politically correct to mark Amoras thing as a solution cause I wouldn’t want to confuse a random person looking for this issue on the forum

You could explain how you solved the issue, then set your own comment as the solution

1 Like

That didn’t seem to work perfectly it still kind of just started and stopped the animation very fast though it did get me on track to half way getting it to work

The stopping works fine and the playing of the animation kind of works but it still gets stopped and restarted after some milliseconds
Bug


Hum:GetPropertyChangedSignal("MoveDirection"):Connect(function()
	
	if Hum.MoveDirection.Magnitude > 0 and WalkingAnim.IsPlaying == false then
		
			WalkingAnim:Play()
			WalkingAnim.Looped = true
		
	end
	
	if Hum.MoveDirection.Magnitude == 0 and WalkingAnim.IsPlaying == true then
		
		WalkingAnim:Stop()
		WalkingAnim.Looped = false

	end
	WalkingAnim.Stopped:Wait()
end)

The only issue I see is in the first if statement.
You typed Isplaying. Change that to IsPlaying. Lua is very case sensitive, which can be annoying. That could still be causing the issue, but if that doesn’t help, please tell me and provide more of the code.

I would also remove the line WalkingAnim.Stopped:Wait().

Must of saw it before I edited it
I did fix the typo and had the same issue
Removing the .Stopped Wait
Helped slightly but its still having the same issue

Wait I just realised I had some of the old code still in the script… they should call me the dumb ducky XD

It works wonderfully now!!!

Thank you for all the help :smiley:

1 Like

I’m glad to have helped!

I don’t want to sound greedy asking this, but can you mark my first post as the solution? Just so people don’t mistake your post as unsolved and try to help unnecessarily. Thanks!

I was gonna break down the Thread in one big message and give everyone credit where its due that way someone looking at the thread can actually understand what’s going on

That is okay. It would still be a good idea to at least mark a post as solution. It keeps the forum neater so people looking really far down don’t bump the post for no reason.

Yeah ima mark the breakdown as the solution

1 Like

Break Down of this thread!

I originally was trying to change a value with a local script then have a Sever script do something with it. This was all for a system of stopping a Idle animation when a walking animation is playing. while trying to solve this issue I was told that you can do that without using remote events By DevNinetailfox. I then decided to scrap the part of the idle animation that was causing me issues.

I later noticed Amora’s code was a easier way of handling walking animations playing. I used to have a Input.Keycode for W, A, S, and D that would play the animation. This was buggy and made a lot of unneeded code

Me and Amora edited Amora’s initial code and got it working perfectly since the original was a little buggy.

All credit goes to Amora for coming up with the Initial Idea! (Thanks Amora :smiley: )

The code is for starting and stopping a players animation dependent on the players magnitude

Here is the code for any who can find use out of it (This is used in a local script)

Hum:GetPropertyChangedSignal("MoveDirection"):Connect(function()
	
	if Hum.MoveDirection.Magnitude > 0 and WalkingAnim.IsPlaying == false then
		
			WalkingAnim:Play()
			WalkingAnim.Looped = true
		
	end
	
	if Hum.MoveDirection.Magnitude == 0 and WalkingAnim.IsPlaying == true then
		
		WalkingAnim:Stop()
		WalkingAnim.Looped = false

	end
end)
1 Like