I need help fixing an issue with my sprinting script

Hi everyone, since I’m not the best at scripting, I’m looking for some help fixing a script that allows players to run and crouch.
The basic idea of it is that you can only sprint if you don’t have a tool out or if you aren’t crouching, while it does work, whenever a player crouches, they gain the ability to run with a tool out.

Here’s a visual demonstration of the problem, as you can see I can run just fine without a weapon, equipping a weapon resets the FOV, lowers the wind volume (which you can’t hear in the demonstration), and stops the animation. But when I crouch and stop crouching, I gain the ability to run with my weapon, which looks like this:

My script has three sounds (Being “Start”, “wind” and “music”), and one ParticleEmitter named “RunParticle” placed in the script.

My script looks like this, I think the issue might be in lines 60 - 62, but I’m not sure.

local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local HUMAN = Character:WaitForChild("Humanoid")
local UIS = game:GetService("UserInputService")
local CanSprint = true
local CanCrouch = true
local Sprinting = false
local Crouching = false
local Animation = Instance.new("Animation")
local Stop = Instance.new("Animation")
local particle = script:WaitForChild("RunParticle");
local hrp = script.Parent:WaitForChild("HumanoidRootPart");
Animation.AnimationId = "rbxassetid://6164106604"
Animation.Parent = player.Character
Stop.AnimationId = "rbxassetid://6163978394"
Stop.Parent = player.Character
local mouse = player:GetMouse()
local function createParticle(cf, t)
	local part = Instance.new("Part");
	part.Size = Vector3.new(4, 4, 4)
	part.Anchored = true;
	part.CanCollide = false;
	part.Transparency = 1;
	part.CFrame = cf;
	part.Parent = game.Workspace;

	local clone = particle:Clone();
	clone.Enabled = true;
	clone.Parent = part;

	local life = clone.Lifetime;
	for i = 0, 1.1, 0.1 do
		clone.Lifetime = NumberRange.new((1-i)*life.Min, (1-i)*life.Max + 0.1);
		wait(t*0.1);
	end

	game:GetService("Debris"):AddItem(part, t);
end


local Animate
local debounce = false



UIS.InputBegan:Connect(function(Key)
	if Key.KeyCode == Enum.KeyCode.LeftControl and CanCrouch then
		if not debounce then
			Crouching = true
			CanSprint = false
			Sprinting = false
			wait(0.1)
			debounce = true
            Animate = HUMAN:LoadAnimation(Animation)
			            Animate:Play()
			HUMAN.WalkSpeed = HUMAN.WalkSpeed - 13
			HUMAN.JumpPower = HUMAN.JumpPower - 24
			HUMAN.CameraOffset =  Vector3.new(0,-1.1,0)
		else
			Crouching = false
			CanSprint = true
			Sprinting = false
			wait(0.1)
			Animate:Stop()
			HUMAN.CameraOffset =  Vector3.new(0,0,0)
			HUMAN.WalkSpeed = HUMAN.WalkSpeed + 13
			HUMAN.JumpPower = HUMAN.JumpPower + 24
			debounce = false
		end  
	end
end) 



local UIS = game:GetService('UserInputService')
local Player = game.Players.LocalPlayer
local Character = Player.Character
UIS.InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift and not debounce and CanSprint and not Sprinting and not Crouching then
		Sprinting = true
		Character.Humanoid.WalkSpeed = 32
		local Anim = Instance.new('Animation')
		Anim.AnimationId = 'rbxassetid://6498752831'
		PlayAnim = Character.Humanoid:LoadAnimation(Anim)
		PlayAnim:Play()
		script.Start:Play()
		CanCrouch = false
		game.Workspace.Camera.FieldOfView = 106
		script.wind.Volume = 0.45
		script.music.Volume = 0.007
		createParticle(hrp.CFrame * CFrame.new(0, -1, -1), .3);
	else
		Sprinting = false
    end
end)

UIS.InputEnded:connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift and CanSprint then
		Character.Humanoid.WalkSpeed = 20
		game.Workspace.Camera.FieldOfView = 100
		script.wind.Volume = 0
		script.music.Volume = 0
		CanCrouch = true
		Sprinting = false
		PlayAnim:Stop()
	end
end)

player.Character.ChildAdded:Connect(function(tool)
	if tool:IsA("Tool") then
		game.Workspace.Camera.FieldOfView = 100
		script.music.Volume = 0
		script.wind.Volume = 0
		CanSprint = false
		Sprinting = false
		CanCrouch = true
		HUMAN.WalkSpeed =  20
		if Crouching then
			HUMAN.WalkSpeed = 7
			CanSprint = false
		end
		if PlayAnim then
			PlayAnim:Stop()
		end
	end
end)

player.Character.ChildRemoved:Connect(function(untool)
	if untool:IsA("Tool") then
		Character.Humanoid.WalkSpeed = 20
		CanSprint = true 
		Sprinting = false
		CanCrouch = true
		if Crouching then
			HUMAN.WalkSpeed = 7
			CanSprint = false
		end
		if PlayAnim then
			Character.Humanoid.WalkSpeed = 20
			PlayAnim:Stop()
		end
	end
end)

player.Character.ChildRemoved:Connect(function(rununtool)
	if rununtool:IsA("Tool") then
		if Sprinting then
			HUMAN.WalkSpeed =  20
			CanSprint = true
			Sprinting = false
			CanCrouch = true
			if Crouching then
				HUMAN.WalkSpeed = 7
				CanSprint = false
			end
			PlayAnim:Stop()
		else
			Character.Humanoid.WalkSpeed = 20
			CanSprint = true
			if PlayAnim then
				Character.Humanoid.WalkSpeed = 20
				PlayAnim:Stop()
			end
		end
	end
end)

Thanks for reading, help is greatly appreciated.

This line seems to have an “else” attached with sets Sprinting to false. This means, when you press any other key while sprinting, it will set Sprinting to false. Likely, you are pressing wasd triggering this value to be false.

1 Like

I would suggest making functions for both entering and exiting sprint. These functions would control the walkspeed, FOV, sound, and any other effects. Then you won’t have any duplicate code, and you can track the bugs down more easily.

1 Like

This wasn’t the issue, but I’ll make the effects into functions so I can easily find where the problems are, thanks for the suggestion.

I now know for a fact that lines 60 - 62 are causing the issue, if a player stops crouching this enables the ability to run, regardless if a tool is out. I don’t know how to fix this, and I’ll keep trying. If anyone knows how to then feel free to reply to this.

The issue has been resolved in Messages with @Gumballixx123, the fix for is was to check if the person has a tool when uncrouching, and if they didn’t, update CanSprint, if they did, don’t update it

Here’s the updated code in InputBegan

UIS.InputBegan:Connect(function(Key)
	if Key.KeyCode == Enum.KeyCode.LeftControl and CanCrouch then
		if not debounce then
			Crouching = true
			CanSprint = false
			Sprinting = false
			wait(0.1)
			debounce = true
            Animate = HUMAN:LoadAnimation(Animation)
			            Animate:Play()
			HUMAN.WalkSpeed = HUMAN.WalkSpeed - 13
			HUMAN.JumpPower = HUMAN.JumpPower - 24
			HUMAN.CameraOffset =  Vector3.new(0,-1.1,0)
		else
			Crouching = false
			if not player.Character:FindFirstChildOfClass("Tool") then
				CanSprint = true
			end
			Sprinting = false
			wait(0.1)
			Animate:Stop()
			HUMAN.CameraOffset =  Vector3.new(0,0,0)
			HUMAN.WalkSpeed = HUMAN.WalkSpeed + 13
			HUMAN.JumpPower = HUMAN.JumpPower + 24
			debounce = false
		end  
	end
end) 
1 Like