Bug with Crouching and Running

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)
1 Like

I would try to set the run variable to false while your crouching,

UIS.InputBegan:Connect(function(input, GPE)
	if not GPE then
		if input.KeyCode == Enum.KeyCode.C then
			IsCrouching = true
             run = false
			print("IsCrouching is True")
		end
	end

end)
1 Like

Can’t you just load the run animation by pasting it into the character Animate script? Saves you some hassle since Roblox does it for you. Looks like the run animation is still playing so either set the run animation to a Movement priority and the crouch to an Action one (if they aren’t this already), or use something like :GetPlayingAnimationTracks to stop the run animation manually. Depending on your animation track priorities honestly I think the best way (Movement and Action priorities) to handle the stuff like crouching while running. You can set the animation priority manually in code or on Roblox.

1 Like

I tried this out and It fixed the second bug

I fixed it. :slight_smile: It had something to do with the other lines of the code in the script. Lines 75 to 88 and the duplicate of it. I just needed to change the if statement for it to only play if the player is running. I had to add this to completely fix it.

UIS.InputBegan:Connect(function(input,gameprocessed)
	-- If you crouch while running
	if IsCrouching == true then 
		if input.KeyCode == Enum.KeyCode.W then
			print("If function was played (Crouch)")
			run = false
			if Track1 then
				Track1.Priority = Enum.AnimationPriority.Movement
				Track1:Stop(0.300000001)
			end
			hum.WalkSpeed = 8
			runs.Value = false
		end
	end
end)

UIS.InputBegan:Connect(function(input,gameprocessed)
	-- If you crouch while running
	if IsCrouching == true then 
		if input.KeyCode == Enum.KeyCode.S then
			print("If function2 was played (Crouch)")
			run = false
			if Track1 then
				Track1.Priority = Enum.AnimationPriority.Movement
				Track1:Stop(0.300000001)
			end
			hum.WalkSpeed = 8
			runs.Value = false
		end
	end
end)
2 Likes

If you fixed it, then you should mark it as solution otherwise people will still try to help you

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.