I’m currently working on a game and I have encountered an issue.
Whenever I crouch, I can still run. Thus making the crouch feature practically useless.
Does anybody know how I could possibly fix it?
Crouch script:
local Player = game.Players.LocalPlayer
local Humanoid = Player.Character:WaitForChild('Humanoid')
local RunAnimation = Instance.new('Animation')
local humanim = Player.Character.Humanoid:LoadAnimation(script.CrouchAnimation)
local humanimid = Player.Character.Humanoid:LoadAnimation(script.CrIdleAnimation)
Running = false
local WalkingSpeed = 16
local CrouchSpeed = 10
function Handler(BindName, InputState)
if InputState == Enum.UserInputState.Begin and BindName == 'RunBind' then
Running = true
Humanoid.WalkSpeed = CrouchSpeed
humanimid:Play()
elseif InputState == Enum.UserInputState.End and BindName == 'RunBind' then
humanim:Stop()
humanimid:Stop()
Running = false
Humanoid.WalkSpeed = WalkingSpeed
end
end
Humanoid.Running:connect(function(Speed)
if Speed >= CrouchSpeed - 1 and Running and not humanim.IsPlaying then
humanim:Play()
Humanoid.WalkSpeed = CrouchSpeed
end
if Speed >= CrouchSpeed + 1 and not Running and humanim.IsPlaying then
humanim:Stop()
Humanoid.WalkSpeed = WalkingSpeed
humanimid:Stop()
elseif Speed < CrouchSpeed - 1 and humanim.IsPlaying then
humanim:Stop()
end
end)
Humanoid.Changed:connect(function()
if Humanoid.Jump and humanim.IsPlaying then
humanim:Stop()
humanimid:Stop()
end
end)
game:GetService('ContextActionService'):BindAction('RunBind', Handler, true, Enum.KeyCode.C)
Run script:
local UIS = game:GetService("UserInputService")
local RunSpeed = 25
local Player = game.Players.LocalPlayer
local Stamina = script.Parent:WaitForChild("CurrentStamina")
local IsSprinting = script.Parent:WaitForChild("IsSprinting")
local Character = workspace:WaitForChild(Player.Name)
local Humanoid = Character:WaitForChild('Humanoid')
local RunAnimation = Instance.new('Animation')
RunAnimation.AnimationId = 'rbxassetid://15904280685'
RAnimation = Humanoid:LoadAnimation(RunAnimation)
local SoundFX = script.RunSound
UIS.InputBegan:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.LeftShift then
if Stamina.Value > 5 then
Player.Character.Humanoid.WalkSpeed = RunSpeed
IsSprinting.Value = true
RAnimation:Play()
SoundFX:Play()
end
end
end)
UIS.InputEnded:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.LeftShift then
IsSprinting.Value = false
Player.Character.Humanoid.WalkSpeed = 16
RAnimation:Stop()
SoundFX:Stop()
end
end)
Thanks for the support in advance.