Stamina server handling

I got a question, I made a module, and this module is replicated from the server to every client. When its running my server receive goes all the way up to 3kb/s. It may not sound like a lot but that’s only for one player. I was wondering what is way to reduce that or a better way of doing this:

local RegenStaminaTask

local IsRegenerating = false
local Interrupted = false

return function (Player)
	local Character = Player.Character
	
	while not Player:GetAttribute("IsLoaded") do
		task.wait()
	end
	
	local OldStamina = Player:GetAttribute("Stamina")

	-- Stamina attribute change connection
	Player:GetAttributeChangedSignal("Stamina"):Connect(function()
		local Stamina = Player:GetAttribute("Stamina")
		local MaxStamina = Player:GetAttribute("MaxStamina")
		local Hunger = Player:GetAttribute("Hunger")

		local RegenAmount = (0.1 / 100) * MaxStamina

		-- Handle zero stamina
		if Stamina <= 0 then
			Character.Humanoid.WalkSpeed = 0
		end

		-- Check if Stamina decreased or player is sprinting/dashing
		if Stamina < OldStamina then
			Interrupted = true
			if RegenStaminaTask then
				task.cancel(RegenStaminaTask)
			end
			IsRegenerating = false
		end

		-- Start a new regeneration task if not already regenerating
		if not IsRegenerating then
			RegenStaminaTask = task.delay(0.5, function()
				Interrupted = false
				IsRegenerating = true

				while not Interrupted and Stamina < MaxStamina do
					task.wait(0.01)

					Stamina = math.min(Stamina + RegenAmount, MaxStamina)
					Hunger = math.max(Hunger - HungerDecrease, 0)

					Player:SetAttribute("Stamina", Stamina)
					Player:SetAttribute("Hunger", Hunger)
				end

				IsRegenerating = false
			end)
		end

		OldStamina = Stamina
	end)

	--Decrease staminaa by a tiny bit to trigger regeneration
	Player:SetAttribute("Stamina", OldStamina - 0.001)
end

I was also wondering maybe its cause its being replicated to every client. I don’t really know. Here is the script that replicates it.

return function (Players : Players)

	local function PlayerAdded(Player)
		local Character = Player.Character

		local function CharacterAdded()	

			for	_, Module in script:GetChildren() do
				if Module:IsA("ModuleScript") then
					task.defer(require(Module),Player)
				end
			end		
		end
		
		Player.CharacterAdded:Connect(CharacterAdded)
		
		if Character then
			CharacterAdded()
		end		
	end	
	
	Players.PlayerAdded:Connect(PlayerAdded)
	
	for _, player in Players:GetPlayers() do
		PlayerAdded(player)
	end
	
end

Any more question let me know.

1 Like

1000kbs = 1mb
1000mb = 1gb

(most computers have 8+ gbs of ram)
(most mobile devices have atleast 4 gbs)

it would take over 300 players to even gain 1mb, which roblox server would just crash atp+you cannot even set server size to 300


you’ll be fine.

I’ve read a lot however, that it should be kept below 50kb/s. As Roblox will just queue up that data causing server lag and increase client ping and lower performance

you are saying memory should be kept under 50 kbs?
(literally impossible)

games should aim for 400-800mb’s of memory.

Not memory the server receive:
image
Aka this, I am referring to networking.

:man_facepalming:
thats the internet receive, if your testing on studio its going to be higher than normal (just like almost every other stat, such as memory etc.)

Its the same in-game as it is in studio, yea thats my bad i forgot the name

image
This is when the regen is active in game. However when its depleting it looks like this:
image