Trying to make a fuctional run system: Sprinting will decrease Stamina, Walking will increase Stamina.
I can’t quite understand how I’m going to add the Stamina part. I haven’t scripted in about 6 months, so I’m really rusty.
-- This is located in StarterCharacterScripts
local UserInputService = game:GetService("UserInputService")
local Character: Humanoid = game.Players.LocalPlayer.Character:FindFirstChild("Humanoid")
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then return end
Character.WalkSpeed = 36
UserInputService.InputEnded:Connect(function(input)
if input == Enum.KeyCode.LeftShift then
Character.WalkSpeed = 16
end
end)
end)
-- Works perfectly fine, don't know how to integrate Stamina into it, though.
Looked around and was unable to find a solution. I tried to make stamina decrease as you held Shift, and vise versa in a for i, v loop, but it just ended up making stamina constantly rise and lower.
Would greatly appreciate it if someone helped me make this, I’m trying to ease my way back into developing as my motivation returns.
Inside the InputBegan event listener, after checking that input.KeyCode is equal to Enum.KeyCode.LeftShift, you can create a thread that will continually decrease the stamina until there isn’t any, then cancel the thread. When the player let’s go of Enum.KeyCode.LeftShift, you can cancel the thread then, also, and increase the stamina with another thread. Then start the cycle again when the player sprints.
I believe you should also do checks inside the threads to stop them in case the player takes a long time to fire an event. For example, check if there is not stamina left to subtract and cancel the thread instead of keeping the thread alive with a while loop inside of it until the InputEnded event fires.
And you should separate the event listeners, instead of setting up a new InputEnded event listener every time the InputBegan event fires.
Right now you are creating unnecessary threads – a thread is created every time an event fires.
Sorry for not getting back to you, but I’ll use the advice you gave me- also, what’s a thread? (Haven’t scripted in awhile, so I don’t know things I probably should.)
I am talking about code that runs separately from other code. This means that, with threads, you can do multiple tasks at the same time. Threads do not yield code outside of themselves.
You can look into the task and coroutine libraries’ documentations for further understanding and knowledge about threads.
Here’s an example of using a thread:
-- task.delay() waits to execute a function after a given duration,
-- but won't yield the lines of code outside of its calling.
task.delay(5, print, "A")
print("B") -- This will be outputted first.
And here’s an example of not using a thread:
task.wait(5)
print("A")
print("B") -- Waits for the line above to be executed first.
-- This is located in StarterCharacterScripts
local UserInputService = game:GetService("UserInputService")
local Character: Humanoid = game.Players.LocalPlayer.Character:FindFirstChild("Humanoid")
local stamina = 100
local maxStamina = 100
local isDown = false
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then return end
isDown = true
while stamina > 0 and isDown == true then
Character.WalkSpeed = 36
stamina = stamina - 1
wait(0.1)
end
UserInputService.InputEnded:Connect(function(input)
if input == Enum.KeyCode.LeftShift then
isDown = false
Character.WalkSpeed = 16
while stamina < maxStamina and isDown == false do
stamina = stamina + 1
wait(0.1)
end
end
end)
end)