Increasing a Player's stamina every second

I am making a stamina system for an upcoming game of mine, however I can’t figure out how I can make it so that the Stamina of the player increases every second on the server side, similar to how health works. If anyone knows a method of doing this please let me know, I have already tried searching up the devforum and tried researching about tick() but regardless, I can’t figure out how I can make it run on each player in the game on the server side.

I do have an idea, what if I were to make a coroutine for every player who joined, which would contain a code increasing the value of the stamina every heartbeat?

1 Like

The architecture requires you to use Players service’s PlayerAdded. Using this event, you can create the stamina. Once that’s added, you need to loop its refill while making sure that the player has not left by checking Player.Parent, avoiding issues with memory after leaving. Optionally, ensure that the player is not in a certain state where stamina is draining.

If it’s bound to character, then you should use CharacterAdded.

This system utilizes the own environment each time the signals are fired. It should not conflict with others if there are multiple players.

1 Like

That could definitely work, however when a new player joins, won’t the player/character parameter of the event change to the next player?

Only if you are incorrectly writing it. Such instance is when you are using the global scope to work with it. Use the function’s environment to its maximum extent instead.

1 Like

Wait, so there’s a way to use this for each player, like making it run separately for each player? Are you talking about using coroutines?

It is not coroutines. It is the signals themselves as I have previously mentioned. Each time it is fired, it is its own environment. Declaring variables within is fine.

1 Like

Well, you could use Remoteevents, but exploiters can fire the event, giving them infinite stamina.

1 Like

I don’t understand what you mean by it has it’s own “environment”.

Environment is created for each scope, meaning that every time you call a function the environment is established within.

1 Like

That wouldn’t work the best as if there is more than one player using the stamina refill then the script would not work as intended.

And how exactly do you reference the environment and run code inside it? Is there a developer hub article on this? Also thanks alot for your help, I really appreciate it!

No, but every time a signal is fired, the environment is run without having the script’s normal execution to interrupted.

Players.PlayerAdded:Connect(function()
    -- this is the function environment
end)

-- this is the global environment
2 Likes

Players.PlayerAdded:Connect(function(Player)
local RunService = game:GetService(“RunService”)
RunService.RenderStepped:Connect(function()
Player.Stamina.Value += 1
end)
end)

The thing is, the player parameter “Players.PlayerAdded:Connect(function(Player)” will change each time a new player joins, so it will start incrementing the stamina value of the most recently joined player.

What if I were to just loop through the players in the game and check which player has stamina less than 100 and if the player does, I increment it by one?

It will not change the player parameter, because it creates new environments for each time it is ran. RunService should not be declared inside the function, but in the global scope. Looping through all the players would be tedious.

2 Likes

Woah! I didn’t know that, I’m going to go ahead and try that! Also do you think I should make a variable inside the function or will the player parameter still work for the player’s environment?

It will still work for each player. It doesn’t conflict between them.

1 Like

Thank you so much for helping me out, I really appreciate it!