How would I optimize remote events?

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.

3 Likes

I dont think it’s a server memory issue, it’s just network latency which unforunately cannot be avoided but you can use less remote events though

1 Like

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)

  1. 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
  1. 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
  1. 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
2 Likes

So, could this also be caused by too many while task.wait() do loops?

1 Like

How much are you communciating back and forth with the client in your game?

1 Like

Depends, if they are never terminated with a break or return statement - then yes.

1 Like

a lot, for example:

game.ReplicatedStorage.Events.inventoryHotbarSwap.OnServerEvent:Connect(function(player, item)
	local hotbarslotsfolder = player:WaitForChild("HotbarSlots")
	local gear = player:WaitForChild("GearSlots")
	local hotbarfull = false
	if game.ReplicatedStorage.Assets.charms:FindFirstChild(item) == nil then
		if hotbarslotsfolder.hotbar1.Value == "" then
			player.HotbarSlots["hotbar1"].Value = item
		elseif hotbarslotsfolder.hotbar2.Value == "" then
			player.HotbarSlots["hotbar2"].Value = item
		elseif hotbarslotsfolder.hotbar3.Value == "" then
			player.HotbarSlots["hotbar3"].Value = item
		elseif hotbarslotsfolder.hotbar4.Value == "" then
			player.HotbarSlots["hotbar4"].Value = item
		elseif hotbarslotsfolder.hotbar5.Value == "" then
			player.HotbarSlots["hotbar5"].Value = item
		elseif hotbarslotsfolder.hotbar6.Value == "" then
			player.HotbarSlots["hotbar6"].Value = item
		elseif hotbarslotsfolder.hotbar7.Value == "" then
			player.HotbarSlots["hotbar7"].Value = item
		elseif hotbarslotsfolder.hotbar8.Value == "" then
			player.HotbarSlots["hotbar8"].Value = item
		elseif hotbarslotsfolder.hotbar9.Value == "" then
			player.HotbarSlots["hotbar9"].Value = item
		elseif hotbarslotsfolder.hotbar1.Value ~= "" and hotbarslotsfolder.hotbar2.Value ~= "" and hotbarslotsfolder.hotbar3.Value ~= "" and hotbarslotsfolder.hotbar4.Value ~= "" and hotbarslotsfolder.hotbar5.Value ~= "" and hotbarslotsfolder.hotbar6.Value ~= "" and hotbarslotsfolder.hotbar7.Value ~= "" and hotbarslotsfolder.hotbar8.Value ~= "" and hotbarslotsfolder.hotbar9.Value ~= "" then
			game.ReplicatedStorage.Events.notification:FireClient(player, "Your hotbar is full!", Color3.fromRGB(255,0,0))
		end
	else
		if gear.gear1.Value == "" then
			player.GearSlots["gear1"].Value = item

		elseif gear.gear2.Value == "" then
			player.GearSlots["gear2"].Value = item

		elseif gear.gear3.Value == "" then
			player.GearSlots["gear3"].Value = item

		elseif gear.gear4.Value == "" then
			player.GearSlots["gear4"].Value = item

		elseif gear.gear5.Value == "" then
			player.GearSlots["gear5"].Value = item

		elseif gear.gear6.Value == "" then
			player.GearSlots["gear6"].Value = item

		elseif gear.gear7.Value == "" then
			player.GearSlots["gear7"].Value = item

		elseif gear.gear8.Value == "" then
			player.GearSlots["gear8"].Value = item

		elseif gear.gear9.Value == "" then
			player.GearSlots["gear9"].Value = item

		elseif gear.gear1.Value ~= "" and gear.gear2.Value ~= "" and gear.gear3.Value ~= "" and gear.gear4.Value ~= "" and gear.gear5.Value ~= "" and gear.gear6.Value ~= "" and gear.gear7.Value ~= "" and gear.gear8.Value ~= "" and gear.gear9.Value ~= "" then
			game.ReplicatedStorage.Events.notification:FireClient(player, "Your equipmentbar is full!", Color3.fromRGB(255,0,0))
		end
	end
	player = nil
	item = nil
end)

this script runs every time the player changes their hotbar items. (i know im super bad at scripting lol)

3 Likes

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

1 Like

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

1 Like

image
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.

2 Likes

Yeah, I actually disabled some unnecessary scripts and the client and server MB consumption went down a ton (it was 1.3G)

1 Like

Decided to make your code shorter for fun:

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)
1 Like

Wow man,this isnt even what i asked for but thanks! Now i dont have to scroll down for 15 minutes until i get to the bottom of my script :skull:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.