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)
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)
[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
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)
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.
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.