Needing help with my running script

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Simple, I just want a partly advanced running script with animations. I also want to learn how to improve the code, if I can

  2. What is the issue? Include screenshots / videos if possible!
    The issue is with the detection of the player jumping, and also the detection if the player has stopped moving, which is used inside a function called Moved1()
    Basically what the function does, is it checks if the player has stopped moving using a vector3 value, if the players move Direction equals to them, stop the running animations, its pretty simple.

1.) Anyway, the issues are, that whenever the player stops running, and starts to move again, the animation has stopped, and the speed has gone back to normal, but the player is still holding down the shift button.
2.) Whenever the player jumps, the animation stops, changed the players speed back to normal, but once they touch the ground again, the animation continued to play, and the speed is still at normal.
3.) If the player has pressed another button while running, the animation will still play, but the speed would go back to the normal walking speed.
4.) When the player moves their camera, their speed goes back to the default speed, and the animation continued to play

Video of bugs.)

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve tried to fix the code by editing the function, changing the speed, and trying to make another function too, the only thing that partly worked (I think) Are the changes of the Humanoid Walkspeed, which changes it back to normal, after the input began has ran through every line of code. (Check code below to view where the placements are)
    Whenever i try to make another function, it doesn’t work, and just make a bunch of red lines instead.

This is the code that I am using. For this script, I’m using it inside of StarterCharacterScripts, and setting it as a local script.
Code:

--SETTINGS--
local DefaultSpeed = 16
local SprintSpeed = 28
local SprintKey = Enum.KeyCode.LeftShift
local UIS = game:GetService("UserInputService")
local PLayers = game:GetService("Players")
local player = PLayers.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local RunAnimation = Instance.new("Animation")
RunAnimation.AnimationId = "rbxassetid://9584328745"
local PlayRunAnim = Humanoid:LoadAnimation(RunAnimation)
local Running = false
local VectorZero = Vector3.new(0, 0, 0)


local function Moved1()		
	if Humanoid.MoveDirection ~= VectorZero then
	else
		print("Stopped Moving")
		PlayRunAnim:Stop()
		Humanoid.WalkSpeed = SprintSpeed
		return
	end
end

Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(Moved1)

Humanoid.WalkSpeed = DefaultSpeed

UIS.InputBegan:Connect(function(key, gameProcessed)
	if key.KeyCode == SprintKey and gameProcessed == false then
		if not Running then
			Running = true
			if Humanoid.MoveDirection.Magnitude > 0 then
				if not PlayRunAnim.IsPlaying then
					Humanoid.WalkSpeed = SprintSpeed
					PlayRunAnim:Play(0.1)
				end
			end
		else
			Running = false
			Moved1()
		end
		return Moved1()
	end
	Humanoid.WalkSpeed = DefaultSpeed
end)

UIS.InputEnded:Connect(function(key)
	if key.KeyCode == SprintKey then
		Running = false
		
		if PlayRunAnim.IsPlaying then
			Humanoid.WalkSpeed = DefaultSpeed
			PlayRunAnim:Stop(0.1)
		end
	end
end)

Humanoid:GetPropertyChangedSignal("FloorMaterial"):Connect(function()
	if Humanoid.FloorMaterial == Enum.Material.Air then
		if Running then
			repeat wait()
				if PlayRunAnim.IsPlaying then
					PlayRunAnim:Stop(0.1)
					
				end
			until Humanoid.FloorMaterial ~= Enum.Material.Air
			if Running then
				if Humanoid.MoveDirection.Magnitude > 0 then
					if not PlayRunAnim.IsPlaying then
						PlayRunAnim:Play(0.1)
					end
				end
			else
				Running = false
				if PlayRunAnim.IsPlaying then
					PlayRunAnim:Stop(0.1)
					Humanoid.WalkSpeed = DefaultSpeed
				end
			end
		else
			if Running then
				if Humanoid.MoveDirection.Magnitude > 0 then
					if not PlayRunAnim.IsPlaying then
						Humanoid.WalkSpeed = SprintSpeed
						PlayRunAnim:Play(0.1)
				end
			end
			end
		end
		end
end)

I have no Idea what else I could add to fix the issues, but if there are none, then thats fine.
Also, whenever I run the code, no errors pop up either (This could be useful or not, idk.)

So, is there any way for me to be able to fix these problems, or can I not do anything at all?

Thanks

Have you tried stopping it when walkspeed is changed back to default?

Thats the problem, when the player stops moving, while still holding down shift, the animations still didn’t work when they started to move again, and the players speed is back to default, and they are still holding down shift. Which is the problem

Edit: I tried moving around some of the lines, and found that when the code is like this

	else
			Running = false
			Moved1()
		end
		return Moved1()
	end
	Humanoid.WalkSpeed = DefaultSpeed
end)

They can run, then stop running, everything would work as normal, but when i do this instead…

	else
			Running = false
			Moved1()
		end
		return Moved1()
	end
end)

Humanoid.WalkSpeed = DefaultSpeed

It makes it so that when the player stops, they dont lose their speed, but the animation doesn’t play.
Because of that, I tried to add a PlayRunAnim:Play() In the function and the animations, and speed, worked perfectly! Only problem with both of these “Solutions”, is that when the player is walking normally, the animation plays, and without the animation codes in the function, when the player lets go of the shift button, the walkspeed stays the same
I’ve added Multiple Humanoid.WalkSpeed = DefaultSpeed at every last line of the code, and nothing has worked.

Is there anything else that could possibly help?

Lots of unnecessary codes,

UIS.InputBegan:Connect(function(key, gameProcessed)
	if key.KeyCode == SprintKey and gameProcessed == false and Humanoid.MoveDirection.Magnitude > 0 and not Running then
		Running = true
		Humanoid.WalkSpeed = SprintSpeed
		PlayRunAnim:Play(0.1)
		return
	end
	Humanoid.WalkSpeed = DefaultSpeed
	if PlayRunAnim.IsPlaying then
		Humanoid.WalkSpeed = DefaultSpeed
		PlayRunAnim:Stop(0.1)
	end
end)

UIS.InputEnded:Connect(function(key)
	if key.KeyCode == SprintKey and Running then
		Running = false

		if PlayRunAnim.IsPlaying then
			Humanoid.WalkSpeed = DefaultSpeed
			PlayRunAnim:Stop(0.1)
		end
	end
end)

Humanoid:GetPropertyChangedSignal("FloorMaterial"):Connect(function()
	if Humanoid.FloorMaterial == Enum.Material.Air then
		if Running then
			if PlayRunAnim.IsPlaying then
				PlayRunAnim:Stop(0.1)
			end
		end
	end
end)

when you change the walkspeed, just stop the animation.

as for holding shift, I’m a bit busy so I will need some time.

Did you intend to stop running if player changes direction?

No, i just needed it to continue if the direction changes

local function onRunning(speed)
	if speed > Humanoid.WalkSpeed then
		PlayRunAnim:Play(0.1)
	else
		PlayRunAnim:Stop()
	end
end

Humanoid.Running:Connect(onRunning)

UIS.InputBegan:Connect(function(key, gameProcessed)
	if key.KeyCode == SprintKey and gameProcessed == false and Humanoid.MoveDirection.Magnitude > 0 and not Running then
		Running = true
		Humanoid.WalkSpeed = SprintSpeed
		return
	end
end)

UIS.InputEnded:Connect(function(key)
	if key.KeyCode == SprintKey and Running then
		Running = false
		Humanoid.WalkSpeed = DefaultSpeed
	end
end)

Humanoid:GetPropertyChangedSignal("FloorMaterial"):Connect(function()
	if Humanoid.FloorMaterial == Enum.Material.Air then
		if Running then
			if PlayRunAnim.IsPlaying then
				PlayRunAnim:Stop(0.1)
                                Humanoid.WalkSpeed = DefaultSpeed
			end
		end
	end
end)

Try this and let me know if it works for you.

Okay, So i tried your newer code that you posted, and there is a big bug in it.

The animations dont play when actually running, and only work then the input has ended

I dont completely know why this is happening, but it could also be with the function maybe? Or even the vector3

Video:

While doing some more digging around with your last code, I also found some bugs in it as well:
-The animations are playing constantly when the player is not moving
-The animation stops for a bit when the player jumps, and when they land on the ground, the animation doesn’t play, and the character speed is back to default.

Video:

So instead, i tried to do some rummaging in my code, and I PERSONALLY think it would be better to look into that code instead, but obviously, if it doesn’t work, I can work out with a new one.
Also yes, you were right about some lines of code being useless at the end.

Including so, I also got it to sprint at the same speed as before, when the player jumps, and after they jump and land

Video:

I’ll keep looking around as well, ill reply if i find something that may be useful

Also, the code where it says HumanoidRootPart isnt a part of the player, thats a different code, so don’t worry about that

The only main problem about all the code, is that when the player changes direction, or moves the camera, the animation stops, or the player isnt really running anymore

Apologies with all the bug, I was in a rush and wasn’t sure what you really wanted.
the problem was that the speed somehow exceeded walkspeed. This should fix it.

local function onRunning(speed)
	if speed > DefaultSpeed+1 then
		if not PlayRunAnim.IsPlaying then
			PlayRunAnim:Play(0.1)
		end
	else
		PlayRunAnim:Stop()
	end
end


Humanoid.Running:Connect(onRunning)

UIS.InputBegan:Connect(function(key, gameProcessed)
	if key.KeyCode == SprintKey and gameProcessed == false and Humanoid.MoveDirection.Magnitude > 0 and not Running then
		Running = true
		Humanoid.WalkSpeed = SprintSpeed
		return
	end
end)

UIS.InputEnded:Connect(function(key)
	if key.KeyCode == SprintKey and Running then
		Running = false
		Humanoid.WalkSpeed = DefaultSpeed
	end
end)

Humanoid:GetPropertyChangedSignal("FloorMaterial"):Connect(function()
	if Humanoid.FloorMaterial == Enum.Material.Air then
		if Running then
			if PlayRunAnim.IsPlaying then
				PlayRunAnim:Stop(0.1)
				Humanoid.WalkSpeed = DefaultSpeed
			end
		end
	end
1 Like

Okay, So i tested out the code, and the beggining part worked perfectly, only problem is with the jump detection. In your code, you forgot an end, and the end), but anyway.
I simply implemented my jump detection with your code, and it worked out greatly!

The only problem I could find is that sometimes the animation plays multiple times when the player goes to either the right or left when running, but besides that, I should be able to fix it.

Another thing that could maybe bug me a bit, is that when the player is holding down shift, the animation doesn’t play when they start to walk. (This only happens if the player isn’t moving when they start to hold down shift) But Anyway, ill try to find a solution to this one too.

If I can’t fix the issues, ill probably just reply again.

Anyways, thanks for the help!

1 Like