Hello, i have this piece of code to handle a stamina bar for a modular game. The problem is that, by what i understand after looking at the developer console, is the major cause of the insane lag this game has, but i just don’t seem to understand why it happens, could i get some tips on how i could make this less laggy?
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Connection = nil
local AddedConnection = nil
local StaminaBar = {}
function StaminaBar:init()
if AddedConnection then
AddedConnection:Disconnect()
end
local Feature = Player.PlayerGui:WaitForChild("GameUI"):WaitForChild("Background"):WaitForChild("Stamina")
local Character = Player.Character
if Character:GetAttribute("MaxStamina") == nil then
Character:SetAttribute("MaxStamina",100)
end
Character:SetAttribute("Stamina",Character:GetAttribute("MaxStamina"))
AddedConnection = Player.CharacterAdded:Connect(StaminaBar.init)
Feature.Number.Text = "Stamina: " .. math.floor(Character:GetAttribute("Stamina")).." / "..Character:GetAttribute("MaxStamina")
Connection = Character:GetAttributeChangedSignal("Stamina"):Connect(function()
local Percent = Character:GetAttribute("Stamina")/Character:GetAttribute("MaxStamina")
TweenService:Create(Feature.Total.Mask,TweenInfo.new(0.2),{Offset = Vector2.new(Percent,0)}):Play()
Feature.Number.Text = "Stamina: " .. math.floor(Character:GetAttribute("Stamina")).." / "..Character:GetAttribute("MaxStamina")
Connection:Disconnect()
end)
end
type StaminaBar = typeof(StaminaBar)
return StaminaBar
for context, this is my first time making a fully modular game, so i don’t know any of the good practices for it
I think your init function for the StaminaBar contains a listener leak (type of memory leak where unnecesary or redundant listeners/connections accumulate over time).
Taking a look at your code, in this line,
since it’s connecting onto itself (it’s recursive), then each new CharacterAdded connection created is never disconnected, and therefore whenever the player spawns a new character, a new connection is created; next time the player dies, the two connected-connections run the init function two times, respectively, and then you have 4 connections to CharacterAdded: for next respawn, each creates 4 more connections so you now have 8, then 8 more so 16, then +16 so 32, +32 = 64, and so on (recursion-based, listener leak).
The solution, I reckon, is to change the :Connect() of that line to :Once() (:Once() does the same as :Connect() just that it disconnects just after the first trigger, so effectively it only hears the event once).
So, use :Once() there instead of :Connect() and let me know how it goes.
On a similar, unrelated note (not related to your issue at hand nor it’s something bad per-se, it’s just a suggestion for simplicity), you can apply another :Once() in here:
Since you’re directly disconnecting that connection on the first run, you can just use :Once() so you don’t have to do the :Disconnect() nor defining the connection at the start, thus improving readability and reducing clutter.
If there is a location on the client where the raw stamina value (Character:SetAttribute('Stamina', ...)) is updated, you could also batch the UI update there, completely cutting out the need for a connection, this would require a migration to an OOP structure, and make a update function (StaminaBar:update(stamina: number) or similar) containing the logic to update the interface, and call that when the stamina is changed.
However if there are multiple sources that set that attribute, what safeLast120 said may be better, either way a :update() function is probably the cleanest implementation, could just do self:update() as well
--LocalScript in StarterPlayerScripts
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local function setupStamina(character)
local Feature = Player.PlayerGui:WaitForChild("GameUI"):WaitForChild("Background"):WaitForChild("Stamina")
if character:GetAttribute("MaxStamina") == nil then
character:SetAttribute("MaxStamina",100)
end
character:SetAttribute("Stamina",character:GetAttribute("MaxStamina"))
local maskTween
local function updateUI()
local stamina = character:GetAttribute("Stamina")
local maxStamina = character:GetAttribute("MaxStamina")
local percent = stamina/maxStamina
Feature.Number.Text = "Stamina: " .. math.floor(stamina).." / "..maxStamina
if maskTween then maskTween:Cancel() end
maskTween = TweenService:Create(Feature.Total.Mask,TweenInfo.new(0.2),{Offset = Vector2.new(percent,0)})
maskTween:Play()
end
character:GetAttributeChangedSignal("Stamina"):Connect(updateUI)
updateUI()
end
Player.CharacterAdded:Connect(setupStamina)
if Player.Character then
setupStamina(Player.Character)
end
local Player = game:GetService(“Players”).LocalPlayer
Only thing I can think of is Stamina being changed improperly in other scripts, and this can be making TweenService run multiple times within mili-seconds which can cause network issues. Add a system within the code that only runs one Tween at a time.
Really? That post didn’t help at all.. I may be losing my touch here. Sure would help if I had all I needed to test with. I don’t see why people always hide that stuff. You do know all a programmer needs to know is the concept, right? To test something like this for laggy, I need to work with what you’re using.
I agree with this, you cant ask us to debug your problem without sharing the full context.
I think it comes down to not wanting people to steal code. But in reality many of us could care less about the code you are sharing. Especially in this case where there is a memory leak.