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.