Sprint script not working

im making a sprint script, but i have no idea whats causing it to not sprint. can anyone help me pls?

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character
local InputServ = game:GetService("UserInputService")
local DB = false

InputServ:IsKeyDown(function(Input)
	if Input == Enum.KeyCode.LeftShift then
		Character:FindFirstChildOfClass("Humanoid").WalkSpeed = 20
		DB = true
	while DB == true do
		if Input ~= Enum.KeyCode.LeftShift then
			DB = false
			break
		else
	task.wait(0.001)
	end
end
	repeat wait() until DB == false
		Character:FindFirstChildOfClass("Humanoid").WalkSpeed = 16
	end
end)

This is wrong using of IsKeyDown.

You’re not updating local Character and also you could do this instead of while true do (this could lead to probably memory leak and if Input ~= Enum.KeyCode.LeftShift wrong input being used could stop this sprint)

I’m sure you’re trying your best to do this but still you need knowledge

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local InputServ = game:GetService("UserInputService")
local DB = false

InputServ.InputBegan:Connect(function(Input:InputObject,gpe:boolean)
	if Input.KeyCode == Enum.KeyCode.LeftShift then
		Character:FindFirstChildOfClass("Humanoid").WalkSpeed = 20
		DB = true
	end
end)

InputServ.InputEnded:Connect(function(Input:InputObject,gpe:boolean)
if Input.KeyCode == Enum.KeyCode.LeftShift then 
DB = false 
Character:FindFirstChildOfClass("Humanoid").WalkSpeed = 16 
end
end)

thanks but may i ask,
InputServ.InputBegan:Connect(function(Input:InputObject,gpe:boolean)
what are the (Input:InputObject,gpe:boolean) for?

the (Input:InputObject,gpe:boolean) are just arguments passed into the function from the event

Input is an InputObject, while gpe stands for gameProcessedEvent (basically if the engine also handled the input as well)

the colons are just type annotations

check documentation on UserInputService.InputBegan if you want a better understanding

1 Like