So my game has been super laggy lately, and this makes me upset because it’s the game I’ve worked the hardest on.
But I can only assume that this is a server memory issue since the client runs fine.
I read that if you nil all your values at the end of each remote event function, like this:
game.ReplicatedStorage.Events.gidstuzelijfec4so3itngrihdf.OnServerEvent:Connect(function(player, bool, item)
-- use the values and stuff and use memory lol
player = nil
bool = nil
item = nil
end)
It will reduce memory leaks?
if this is true, how can I utilize this?
if not; can someone fact check this?
I doubt the garbage collector isn’t doing this automatically. Also, I’m 99% sure the lag isn’t coming from something as simple as this but from the general structure and communication between your game systems.
This doesn’t really help with memory leaks as it just removes a reference (which would be killed after the thread dies either way). You should look for the following issues your code may have:
As others have stated, it depends on the exact issue you’re having (make sure that memory usage is actually increasing endlessly as the server runs)
A table outside of the function that is never cleared and just stores items forever, leaking memory
local memLeak = {}
local function onServerEvent(player: Player)
local eventTime = memLeak[player]
if eventTime and tick() - eventTime < 0.2 then
return -- demostration with a remote debounce
end
memLeak[player] = tick()
-- ^^ There's our memory leak, it will keep a reference to the Player object
-- until the server dies. To counter such issues, use attributes OR actually
-- clean out tables after the entries are no longer necessary.
-- For example, with Players.PlayerRemoving
end
A never-ending while loop (please do note that the conditions may not be as simple as this and you should check all of the existing loops if possible)
local function memLeak(character: Model)
while character do
-- ^^ Memory leak here! The variable will not turn into nil.
-- In this specific case, "while character and character.Parent do" will work
-- code here
task.wait()
end
end
local function onServerEvent(player: Player)
if player.Character then
task.spawn(memLeak, player.Character)
-- ^^ Spawns a new thread which will never terminate.
-- Doesn't have to be in a new thread too, this will also cause it vv
memLeak(player.Character)
end
end
Connections never disconnected.
local function onPlayerAdded(player: Player)
local function onCharacterAdded(character: Model)
RunService.Heartbeat:Connect(function()
end)
-- ^^ Memory leak! Will never disconnect.
end
player.CharacterAdded:Connect(onCharacterAdded)
end
Well definitely not the most efficient way to make a hotbar system but just keep in mind that having the client do stuff immediately is fine but you just have to do verification checks on the server
LOL, so anyways, what you mean is I can really dumb down all my server stuff and just make it run on the client then check if that stuff is actually true?
Yeah well, you can have a copy of the toolbar on the client and also one on the server if that makes sense, so clients can see the changes instantly on their end but then you can also do veirfication checks on the server insuring the client isn’t doing anything weird, but of course their are always other ways to do it aswell
Use the Performance Stats feature to see if your client is actually getting lots and lots of data from the server. If not, the issue probably lies in the server leaking memory.
game.ReplicatedStorage.Events.inventoryHotbarSwap.OnServerEvent:Connect(function(player, item)
local isHotbar = not game.ReplicatedStorage.Assets.charms:FindFirstChild(item)
local category = isHotbar and player:WaitForChild("HotbarSlots") or player:WaitForChild("GearSlots")
local playerCategory = isHotbar and player.HotbarSlots or player.GearSlots
local message = isHotbar and "hotbar" or "equipmentbar"
local children = category:GetChildren()
for i, v in pairs(children) do
if v.Value == "" then
playerCategory[v.Name].Value = item
break
elseif i == #children then
game.ReplicatedStorage.Events.notification:FireClient(player, "Your "..message.." is full!", Color3.fromRGB(255,0,0))
end
end
end)