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)
try this:
humanoid.Jumped:Connect(function(active) if active then --remove stamina end end)
@Dudeguy90539's answer is not correct, and does not work. This is how you do it:
humanoid.Jumping:Connect(function(isActive) print("Jumping: ", isActive) if isActive then -- lower stamina if jumping == true stamina = stamina - 25 -- edit this value end end)
This code, when run from a LocalScript parented to a Player.Character will listen to the Humanoid’s Jumping action event and print when it is fired. If this helped you figure it out, please mark it as the answer, ty :)