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!