Fixing Sprint Script

I was following a tutorial on how to make a sprint and crouch script by (gamer m8) and it works only problem is when you press shift and let go you are still allowed to sprint, and when you press shift again you are not allowed to sprint. How do I fix this problem. Basically I want the player to hold shift to run and I was going to make a toggle sprint setting later
Script:

local UserInputService = game:GetService("UserInputService")
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local isRunning = false

UserInputService.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		if not isRunning then 
			isRunning = true
			Humanoid.WalkSpeed = 32
		else
			Humanoid.WalkSpeed = 16
			isRunning = false
		end
	end
end)

Video:

You should make use of the InputEnded event as well to reset your script in order for the InputBegan event to be able to start it again.

1 Like

I want to note that you might want to use gameProcessed, as without gameProcessed players typing in chat will be affected too.

1 Like

where can i read about of learn about (gameProcessed)

Let me save you the time, it is quite simple.
With GameProcessed:

local UserInputService = game:GetService("UserInputService")
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local isRunning = false

UserInputService.InputBegan:Connect(function(key, gameProcessed)
if gameProcessed then
	if key.KeyCode == Enum.KeyCode.LeftShift then
		if not isRunning then 
			isRunning = true
			Humanoid.WalkSpeed = 32
		else
			Humanoid.WalkSpeed = 16
			isRunning = false
		end
	end
end
if not gameProcessed then
return
end
end)
1 Like

Thank you so much really appreciate it, but where can I read about gamProcessedEvent. I was looking on Roblox Developer api and cant find it

Since it’s an argument passed by InputBegan, you would find it there.
https://developer.roblox.com/en-us/api-reference/event/UserInputService/InputBegan

[gameProcessedEvent] indicates whether the game engine internally observed this input and acted on it. Generally this refers to UI processing, so if a button was touched or clicked from this input, gameProcessedEvent would be true . This is also true for input events connected via ContextActionService

1 Like

When I run the code it doesn’t work now when i press shift i cant run

local UserInputService = game:GetService("UserInputService")
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local isRunning = false

UserInputService.InputBegan:Connect(function(key,gameProcessedEvent)
	if gameProcessedEvent then
		if key.KeyCode == Enum.KeyCode.LeftShift then
			if not isRunning then 
				isRunning = true
				Humanoid.WalkSpeed = 32
			else
				Humanoid.WalkSpeed = 16
				isRunning = false	
				
			end
		end
	end
	if not gameProcessedEvent then
		return
	end
end)

Oh never mind i found another tutorial but thank you evenryone for your help,i cant wait to be able to answers my own problems lol

Considering that this gamer m8 dude you were watching didn’t list GPE/game processed event nor did they explain the code, I don’t recommend you using their tutorials.

Your not learning scripting when you are bluntly copying it, your learning scripting when you can understand the code.

1 Like

I definitely agree and that’s why I like going over YouTube scripting tutorials. I have html , JavaScript and C++ coding background so I understand coding its just some things in lua I am still trying to learn. so if you think I am just coping code that is not the case I like going over others peoples code and reasoning why they arranged the code the way they did , and asking my self why does that code work. And even going as far as changing things in the code so see what would happen. So thank you for your input but I don’t just copy code and call it a day, the only way i learn is by look at other ppl code and practicing, not only that i read the developer api almost daily. But thank you for your concern ik there is a few kids that just copy code and call it a day.

1 Like