Round System Stopping

Hello! I just remembered something! When I created the two connections for player.AncestroyChanged and humanoid.Died, remember to wrap them in a table which contains the connections (we want to disconnect these for that player).

so it would look more like this:

connections[player.UserId.."Left"] = player.AncestryChanged:Connect(function()
    removePlayer(player)
end)
	
connections[player.UserId.."Died"] = character.Humanoid.Died:Connect(function()
	removePlayer(player)
end)

And change this:

local function removePlayer(player)
	local playerFound = table.find(activePlayers, player)
	if (playerFound) then
		table.remove(activePlayers, playerFound)
        connections[player.UserId.."Left"]:Disconnect()
        connections[player.UserId.."Died"]:Disconnect()
	end	
end

We want to store the connections so we can disconnect them, preventing them from being invoked when the player dies/leaves but they’re not even in the game (the minigame not the session) anymore. This is also useful to prevent memory leaks, having signals/connections that are no longer needed by the game but are still being computed which takes memory.
You can read about memory leaks here:
Garbage Collection and Memory Leaks in Roblox - What you should know

That’s why I included this:

-- Cleaning up connections
for _, connection in pairs(connections) do
	connection:Disconnect()
end

but I forgot about tagging the connections in the first place. Anyways.

So the way how this system works is it uses RBXScriptConnections to immediate response, looping for conditions has it’s downsides:

  • Reconnecting Signals for no reason.
  • Having to wait a second before it checks the conditions, the player would have to wait for a late response which could make or break a game in those clutch moments.

When the round begins we create two connections, one for when the player left and one for when the character dies. AncestryChanged() fires when the instance has been parented somewhere else, in this case when the player leaves they’re parented to nil. Humanoid.Died() is pretty self explanatory, invokes (invoking and firing mean the same thing. Meaning, calling something) when the player has died. When the player leaves or dies we want to remove them from the table which depicts other conditions, i.e the number of players left or winner results. The round condition for checking the number of players still has to wait a maximum of 1 second before it’s checked again.

As for the reset vote code, I’d need to look into that to understand what’s going on and the clashes they make

1 Like