Hello! I need some help with this script, I need to implement a stamina system that when I press the “F” key the stamina starts to go down -15, the maximum stamina is 100 and that if it reaches 0 it starts to go up to 100, how could make this possible?
it’s a normal stamina system, please help!
The code:
-- Input system
UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
-- Frame Call
local Frame = script.Parent
UserInputService.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.F then
Frame.Size = UDim2.new(0, 52,.31, 0)
end
end)
You could create a Variable that’s named Stamina, perhaps?
Adding onto your code:
-- Input system
UserInputService = game:GetService("UserInputService")
local Stamina = 100
local MaxStamina = 100
local player = game.Players.LocalPlayer
-- Frame Call
local Frame = script.Parent
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if input.KeyCode == Enum.KeyCode.F and not gameProcessed and Stamina > 0 then
local Difference = Stamina/MaxStamina
Stamina = Stamina - 15
Frame.Size = UDim2.new(0, Difference * 52,.31, 0)
end
end)
I haven’t personally experimented with UserInputService that much, but I’m guessing it would be somewhat like this
Thanks you so much! i have another questions (sorry about that jeje) How can I make if the stamina is at 0 it increases to 100 and if the stamina is less than 100, increase by +5 until it reaches 100?
I think you can use the InputEnded function to check if you want the Stamina to slowly regen back
UserInputService.InputEnded:Connect(function(input, gameProcessed)
if input.KeyCode == Enum.KeyCode.F and not gameProcessed then
if Stamina == 0 then
Stamina = 100
elseif Stamina < 100 then
repeat
wait(0.5)
Stamina = Stamina + 5
until Stamina == 100
end
end
end)
You’ll need to experiment around with InputBegan & InputEnded tbh