So I've made a stamina script in which stamina will be decreased as you sprint, however I also want to add a feature in which that when you jump, a block of stamina will be removed. I have no clue how to incorporate it into my script and help would highly be appreciated.
local players = game:GetService("Players")
local inputService = game:GetService("UserInputService")
local player = players.LocalPlayer
local char = player.Character or player.CharacterAdded:wait()
local humanoid = char:WaitForChild("Humanoid")
local stamina = 100
local Interface = {}
do
local playerGui = player:WaitForChild("PlayerGui")
local gui = playerGui:WaitForChild("Stamina")
local background = gui:WaitForChild("Background")
local bar = background:WaitForChild("Bar")
local UDim2_new = UDim2.new
function Interface:UpdateStamina()
bar.Size = UDim2_new(stamina/100, 0, 1, 0)
end
end
inputService.InputBegan:connect(function(input, event)
if (event or stamina <= 0) then return end
local key = input.KeyCode.Name
if (key == "LeftControl") then
humanoid.WalkSpeed = 24
repeat
if (humanoid.MoveDirection.Magnitude > 0.1) then
stamina = stamina - 1
Interface:UpdateStamina()
end
wait(0)
until not inputService:IsKeyDown(Enum.KeyCode.LeftControl) or stamina <= 0
humanoid.WalkSpeed = 16
if (stamina <= 0) then
wait(2)
else
wait(1)
end
while (not inputService:IsKeyDown(Enum.KeyCode.LeftControl) and stamina < 100) do
stamina = stamina + 2
Interface:UpdateStamina()
wait(0)
end
end
end)

3 comments
reeeeee, people spoon feeding answers. Explain your answer!!!!
Srry im new to the forum.
The Humanoid.Jumped event fires whenever the humanoid jumps. Using this, you can deduct stamina from the player whenever the humanoid jumps, and not just with the spacebar (spamming would cause stamina loss without an actual jump). for a full explanation of this event, here is a link to the wiki page: https://developer.roblox.com/en-us/api-reference/event/Humanoid/Jumping