so i have this stamina system but its incredibly buggy and requires me to either jump really quickly or hold down space to get the bar to go down. can i get some tips or anything to improve this to work on jump rather than when the space bar is pressed? i also want this to work for mobile
code:
local bar = script.Parent
local stamina = 100
local staminaRate = 0.1
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local isJumping = false
local uis = game:GetService("UserInputService")
uis.InputBegan:Connect(function(key, gameProcessed)
if gameProcessed or stamina == 0 then return end
if key.KeyCode == Enum.KeyCode.Space then
isJumping = not isJumping
if isJumping then
humanoid.JumpPower = 50
else
humanoid.JumpPower = 0
end
end
end)
uis.InputEnded:Connect(function(key, gameProcessed)
if gameProcessed or stamina == 0 then return end
if key.KeyCode == Enum.KeyCode.Space then
isJumping = not isJumping
if isJumping then
humanoid.JumpPower = 50
else
humanoid.JumpPower = 0
end
end
end)
while wait() do
if stamina == 0 and isJumping then
isJumping = false
humanoid.JumpPower = 0
wait(5)
end
if isJumping then
stamina = stamina - 5
wait(0.1)
else
stamina = stamina + 5
wait(0.1)
end
stamina = math.clamp(stamina, 0, 100)
bar:TweenSize(UDim2.new((1/100) * stamina, 0, 1 ,0), Enum.EasingDirection.InOut, Enum.EasingStyle.Quint, 0.5)
end