[CLOSED] UserInputService function, doesn't change the WalkSpeed

I have a crawling script, which is linked down bellow. I want it to change the WalkSpeed to the set number when the player is crawling and when he isn’t it should change it back to the default speed. I have tried everything and I couldn’t get this to work I don’t know why.

I have tried fixing it with

 game["Run Service"].RenderStepped:Connect(function()

and that did kinda work, but I have another function which is for sprinting that uses it, and it got messed up. Are there any other solutions to this?

Here is the code:

local animator = humanoid:WaitForChild("Animator")

local crawling = false
local defaultSpeed = 16
local crawlingSpeed = 7
local crawlKey = Enum.KeyCode.C
local animI = Instance.new("Animation")
animI.AnimationId = "rbxassetid://14166499405"
local animW = Instance.new("Animation")
animW.AnimationId = "rbxassetid://14167146354"
local animationI = animator:LoadAnimation(animI)
local animationW = animator:LoadAnimation(animW)

local uis = game:GetService("UserInputService")
local sprinting = false

uis.InputBegan:Connect(function(keyCode, _gameProcessed)
	if not _gameProcessed then
		if keyCode.KeyCode == crawlKey then
			if not sprinting then
				crawling = true
				if crawling == true then
					humanoid.WalkSpeed = crawlingSpeed
					if humanoid.MoveDirection.Magnitude > 0 then
						animationW:Play()
					else
						animationI:Play()
					end
					humanoid.Running:Connect(function(movementSpeed)
						if crawling then
							if movementSpeed > 0 then
								if not animationW.IsPlaying then
									animationI:Stop()
									animationW:Play()
								end
							else
								if animationW.IsPlaying then
									animationW:Stop()
									animationI:Play()
								end
							end
						end
					end)
				else
					humanoid.WalkSpeed = defaultSpeed
				end
			else
				crawling = false
				humanoid.WalkSpeed = defaultSpeed
			end
		end
	end
end)

uis.InputEnded:Connect(function(keyCode, _gameProcessed)
	if not _gameProcessed then
		if keyCode.KeyCode == crawlKey then
			crawling = false
			humanoid.WalkSpeed = defaultSpeed
			animationI:Stop()
			animationW:Stop()
			
		end
	end
end)

Help is really appreciated, have a nice day guys!

Use print statements to see what conditions are not met. For run service, I’m pretty sure you’re supposed to do game:GetService("RunService").RenderStepped:Connect(function()

1 Like

Interesting, it could be that the code is held back from :GetService and the other code ran faster, resulting in a possible error. Back to the original problem, I realize you have this code inside your if statement.

You need to disconnect this function once it is finished being used. If you keep connecting this to your humanoid, unexpected errors will occur.

1 Like