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

How do I make a stamina bar go down when someone walks/jumps?

I am trying to make a stamina bar go down when someone walks/jumps. I have tried to write something, but it doesn't work.

local Frame = game.StarterGui.VisibleStatus.Frame.Stamina.Frame.Frame

game:GetService("UserInputService").InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift or input.Keycode == Enum.KeyCode.Space then
		Frame.Size = UDim2.new(Frame.Size.X.Offset - 15, 0, 1, 0)
	end
end)

If you have a solution, I'd be very grateful if you shared it with me. Thanks in advance.

1 answer

  • eternal loop -- check if is Runnin or jumping. -- do stuff
while wait(0.5) do
	if humanoid:GetState() == Enum.HumanoidStateType.Running or
	   humanoid:GetState() == Enum.HumanoidStateType.Jumping then
		-- do your maths
	end
end

To do what you asked me, first create an IntValue inside the character indicating theyr stamina number and change its value to 100 (max stamina)

then in that loop above, add stamina decay. Remember that is not good to change values from local, so use remote event to do it.

local decay = 1
local MaxStamina = 100
local RemoteEvent = game.ReplicatedStorage.RemoteEvent

if character:FindFirstChild("Stamina") == nil then
	Stamina = Instance.new("IntValue", character)
	Stamina.Value =  MaxStamina
end

while wait(0.4) do
	if humanoid:GetState() == Enum.HumanoidStateType.Running or
	   humanoid:GetState() == Enum.HumanoidStateType.Jumping then
		-- ask server to low stamina value
		RemoteEvent:FireServer(StaminaDecay, decay)
	end
end

In the server script, make the function to listen the event:

RemoteEvent.OnServerInvoke:Connect(function(player, request, data) -- player is automatically passed, the other 2 parameters will get the value that the local script passed to them.
	if request == "StaminaDecay" then -- data here is "decay"
		player.Character.Stamina.Value = player.Character.Stamina.Value - data
	end
end)

in local script, add an event that will run everytime the Stamina value has changed, this will make the stamina bar display a value based on the Stamina int value:

local barsize = Frame.Size.X.Scale -- used your variable Frame here
Stamina.Changed:connect(function()
	
if Stamina.Value < 0 then
		RemoteEvent:FireServer("AdjustStamina")
		-- prevents from going lower then 0
	end

	if Stamina.Value > MaxHunger then
		RemoteEvent:FireServer("AdjustStamina", MaxHunger)
		-- prevents from going greatter then maxhunger	
	end

	Frame:TweenSize(UDim2.new((barsize*Stamina.Value)/MaxStamina,
				Frame.Size.X.Offset, Frame.Size.Y.Scale, Frame.Size.Y.Offset),
				"Out", "Linear", .2, true) -- complicated maths to set size of bar
end)

In the server script add the function for adjust stamina:

RemoteEvent.OnServerInvoke:Connect(function(player, request, data) -- player is automatically passed
	
	if request == "StaminaDecay" then -- data here is "decay", but can be any one that you pass through RemoteEvent:FireServer("StaminaDecay", THIS)
		player.Character.Stamina.Value = player.Character.Stamina.Value - data
	
elseif request == "AdjustStamina" then -- data here can be MaxRunger or nil.
		if data then -- not nil, is MaxHunger
			player.Character.Stamina.Value = data
		else -- nil
			player.Character.Stamina.Value = 0
		end
	end
end)

Thats it, i didnt tested the code so you may have to solve some bugs through the output. Anytime you want an action to low your stamina, call the remote function with parameters ("StaminaDecay", number to be subtracted).

15 comments

0

Would you be able to explain how I'd be able to change the frame's size depending on the action?

0

Sorry if I'm asking for much, I'm new to this, and not experienced at all.

0

you mean, you want it to take more stamina to jump them to run or something like this?

0

I imagined it like, Jumping takes more, but running takes stamina every 0.4 seconds or something. And if stamina is empty, you cannot jump nor run.

0

ok ill wdit the answer with what i can

0

Thanks

0

done

0

Alright, I'll test it.

0

i was editing some points, make sure to copy the last version xD

0

Also, I dont know how the bar you made looks. That resizement is suposed to work in a bar that decays from right to left, and I used in a Image gui, not a frame..

0

I just remembered that GUI tends to load faster then the character, if this happns will case error in the script when trying to get the char, if it happens you can use "repeat wait() until player.Character"

0

The frame is supposed to go from right to left, and it will be changed to an image later. This is a bit too complicated for me but I'll try to follow instructions, also might take a while since I have to do my homework too.

0

Yeah, life bars are kindda annoying.

0

Well, I've been at this for 25 minutes, I have no clue what I'm doing, I'm sure this would work if I knew how to set it up. These instructions are a bit hard for me to understand, but I guess I don't have time anymore for today, maybe I'll retry tomorrow.

0

try searching about remote events first then, they are very important.

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