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.