Character wont slow down when the character crouches

Hi, I am trying to make the character go slower when the character crouches but I can’t do it. I have two codes one of them is for running and the other one is for crouching.

running code:
local inputService = game:GetService(“UserInputService”)
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:wait()

local normalSpeed = script.Parent.Value -- The player's speed while not sprinting
local sprintSpeed = script.Parent.Value + 5-- The player's speed while sprinting
local sprinting = false

inputService.InputBegan:Connect(function (key)
	if key.KeyCode == Enum.KeyCode.LeftShift or key.KeyCode == Enum.KeyCode.RightShift then
		running = true
		if char.Humanoid then	
			char.Humanoid.WalkSpeed = sprintSpeed
		end
	end
end)

inputService.InputEnded:Connect(function (key)
	if key.KeyCode == Enum.KeyCode.LeftShift or key.KeyCode == Enum.KeyCode.RightShift then
		running = false
		if char.Humanoid then	
			char.Humanoid.WalkSpeed = normalSpeed
		end
	end
end)

crouching code:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local Animate 
local Humanoid = player.Character:FindFirstChild('Humanoid')
local UserInputService = game:GetService("UserInputService")
local KeyPressed = 0
local Speed = game.StarterGui.Folder.Value

UserInputService.InputBegan:Connect(function(inputObject)
    if inputObject.UserInputType == Enum.UserInputType.Keyboard then
		local value = inputObject.KeyCode.Value 
		if value == 305 and KeyPressed == 0 or value == 306 and KeyPressed == 0 then
			local Animation = Instance.new("Animation", player.Character)
			Animation.AnimationId = "rbxassetid://animationAssetId"
 			Animate = Humanoid:LoadAnimation(Animation)
  			Animate:Play()
			KeyPressed = 1
                    Speed.Value = 15
		elseif value == 305 and KeyPressed == 1 or value == 306 and KeyPressed == 1 then
			local Animation = Instance.new("Animation", player.Character)
			Animate:Stop()
			Animation.AnimationId = "rbxassetid://animationAssetId"
 			Animate = Humanoid:LoadAnimation(Animation)
  			Animate:Play()
			KeyPressed = 2
                    Speed.Value = 13
		elseif value == 305 and KeyPressed == 2 or value == 306 and KeyPressed == 2 then
                    Animate:Stop()
			KeyPressed = 0
                    Speed.Value = 16
		end
	end
end)

I tried printing the SpeedValue from the running code and it said 16 even though I changed it and I could see that it was changing when I clicked on it.
the running code is in starterGui and the crouching code is in startercharacterscripts.

That’s because you’re setting the variables normalSpeed and sprintSpeed when the script runs. The original value is set to the variable and therefore any changes made to the value have no effect.

If you want to use your value method you’ll have to check the speed from within the function.
E.g.

local inputService = game:GetService(“UserInputService”)
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:wait()
local SpeedValue = script.Parent
local sprinting = false

inputService.InputBegan:Connect(function (key)
	if key.KeyCode == Enum.KeyCode.LeftShift or key.KeyCode == Enum.KeyCode.RightShift then
		running = true
		if char.Humanoid then	
			char.Humanoid.WalkSpeed = SpeedValue.Value + 5
		end
	end
end)

Another solution is to remove the value entirely and implement your sprint system into the crouch script. It’d allow you to just change a variable

1 Like

Ty It worked I checked the code changed some stuff the problem was that I was not changing the right Value I was changing the value at StarterGui but I should of been changing the Value at the PlayerGui but your code helped too thanks.