Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to remove stamina when jumping?

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)

2 answers

✓ Accepted answer

try this:

humanoid.Jumped:Connect(function(active)
	if active then
		--remove stamina
	end
end)

3 comments

0

reeeeee, people spoon feeding answers. Explain your answer!!!!

0

Srry im new to the forum.

0

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

@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 :)

2 comments

1

XD XD I meant jumping XD XD

0

ty for catching that XD XD

This discussion is preserved as it was. New answers, comments, and votes are closed.