How do I make some events run in the specific way I want them to?

For example, I don’t want DescendantAdded and DescendantRemoving of a character, to fire when a character dies and respawns.

I also want CharacterAdded event to fire the first time the character loads, not every time.

The reason to that, is that in my script, I have this.

function playerCollision.run()
	local function onCharacterAdded(character)
		playerCollision.setCharacterCollisionGroup(character, "Players")
		
		character.DescendantAdded:Connect(function(object)
			playerCollision.setPartCollisionGroup(object, playerCollision.currentCharacterCollisionGroup[character])
		end)
		
		character.DescendantRemoving:Connect(playerCollision.resetCollisionGroup)
	end

	local function onPlayerAdded(player)
		player.CharacterAdded:Connect(onCharacterAdded)
	end

	Players.PlayerAdded:Connect(onPlayerAdded)
end

I don’t think I need to explain what the playerCollision functions do, their names are self-explanatory.

I was thinking, if I ever want to implement a way in my game, for the character to have more lives than one in a round, it will be impossible because when the character is in a round, I set it to another collision group, and once it respawns it is automatically set to another one, which would break my game, which is why I only want it to set the first time it loads.

As I’m writing, I realised that I don’t need the Descendant events to work the way I described, I realised my code handles it the right way. However, I would still like to know how to do that, for future reference.

And I still need to know how to make the characterAdded event fire once.

Might seem like a stupid question and the solution might be obvious, but I haven’t done much for a long time, and have come back recently.

https://developer.roblox.com/en-us/api-reference/property/Players/CharacterAutoLoads

CharacterAutoLoads was made specifically for this use case
“This can be useful in games where players have finite lives, such as competitive games in which players do not respawn until a game round ends.”

Tip: You can assign connections to a variable and call :Disconnect() on the variable to remove the connection.

Example:

local PartTouch -- creates the variable, then assign it like so:
PartTouch = part.Touched:Connect(function(hit)
     print(part.Name, "was touched!")
     PartTouch:Disconnect()
end)

The connection will run only once even if you touch the part multiple times.

Additional info: https://developer.roblox.com/en-us/articles/events

Assign the functions being connected to events to variables and then call “Disconnect()” and those same variables to disconnect the functions from the events they are connected to, you can later call “Connect()” again to reconnect the function to any particular event.