The video shows the 2 bugs that I have been trying to fix. I sent the code of the Sprint and Crouch script below. The first time I crouch is how it is supposed to be the other 2 times I crouch isn’t.
Sprint
local UIS = game:GetService("UserInputService")
-- Tick and Player
local lastTime = tick()
local player = game.Players.LocalPlayer
-- Humanoid and Run Vari's
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local run = false
local IsCrouching = false
local Track1
-- Varis and Functions
local WKey = Enum.KeyCode.W
local SKey = Enum.KeyCode.S
local SKeyHeld = false
-- Bugs
UIS.InputBegan:Connect(function(input, GPE)
if not GPE then
if input.KeyCode == Enum.KeyCode.C then
IsCrouching = true
print("IsCrouching is True")
end
end
end)
UIS.InputEnded:Connect(function(input, GPE)
if input.KeyCode == Enum.KeyCode.C then
if not GPE then
IsCrouching = false
print ("IsCrouching is False")
end
end
end)
-- Main Scripting
local runs = char:WaitForChild("Running")
UIS.InputBegan:Connect(function(input,gameprocessed)
if input.KeyCode == Enum.KeyCode.W then
if char:FindFirstChild("Ragdoll") then return end
local now = tick()
local difference = (now - lastTime)
-- 0.5 seconds till you can press w so you can start running.
if difference <= 0.5 and IsCrouching == false and SKeyHeld == false then
run = true
Track1 = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Run)
Track1:Play(0.500000001)
wait (0.1)
hum.WalkSpeed = 24.5
runs.Value = true
end
lastTime = tick()
end
end)
-- Bug Fixing
UIS.InputBegan:Connect(function(input,gameprocessed)
-- If you press S while running
if input.KeyCode == Enum.KeyCode.S then
SKeyHeld = true
run = false
if Track1 then
Track1:Stop(0.300000001)
end
hum.WalkSpeed = 16
runs.Value = false
end
end)
UIS.InputEnded:Connect(function(input,gameprocessed)
-- If you press S and hold it then press W twice
if input.KeyCode == Enum.KeyCode.S then
SKeyHeld = false
run = false
if Track1 then
Track1:Stop(0.300000001)
end
hum.WalkSpeed = 16
runs.Value = false
end
end)
UIS.InputBegan:Connect(function(input,gameprocessed)
-- If you crouch while running
if IsCrouching == true then
run = false
if Track1 then
Track1:Stop(0.300000001)
end
runs.Value = false
end
end)
-- Ending the Run
UIS.InputEnded:Connect(function(input,gameprocessed)
if input.KeyCode == Enum.KeyCode.W then
run = false
if Track1 then
Track1:Stop(0.300000001)
end
hum.WalkSpeed = 16
runs.Value = false
end
end)
And this is the Crouch Script
--Player--
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Player = Players.LocalPlayer
local Character = script.Parent
local Humanoid = Character.Humanoid
local Mouse = Player:GetMouse()
local Camera = game.Workspace.Camera
local Storage = ReplicatedStorage.Storage
local RunService = game:GetService("RunService")
local runs = Character:WaitForChild("Running")
--Crouch--
local CrouchAnimBackup = "rbxassetid://18764156374"
local SoundsFolder = Storage.Sounds
local CrouchSounds = SoundsFolder.Crouch
local GetUpSound = SoundsFolder.GetUp
local AnimationFolder = Storage.Animations
local CrouchAnimation = Humanoid:LoadAnimation(AnimationFolder.Crouch)
local function IsPlayerMoving()
if Humanoid.MoveDirection.Magnitude == 0 then
end
end
RunService.RenderStepped:Connect(IsPlayerMoving)
--Main Script--
Mouse.KeyDown:Connect(function(Key)
if Key == "c" then
CrouchAnimation:Play()
CrouchAnimation.Priority = Enum.AnimationPriority.Action
Humanoid.WalkSpeed = 8
Camera.CameraSubject = Character.Head;
local function IsPlayerMoving()
if Humanoid.MoveDirection.Magnitude == 0 then
CrouchAnimation:AdjustSpeed(0)
else
CrouchAnimation:AdjustSpeed(1)
end
end
CrouchSounds:Play()
RunService.RenderStepped:Connect(IsPlayerMoving)
end
end)
Mouse.KeyUp:Connect(function(Key)
if Key == "c" then
CrouchAnimation:Stop()
Humanoid.WalkSpeed = 16
GetUpSound:Play()
end
end)