How to call a function based on players in a table

I have a table that could have up to three players in it (or as little as one player). I have a :CharacterRemoving event for when the player dies, at the moment I have a :CharacterRemoving even for player1, player2 and player3. The issue with this is if there are only player1 and player2 that actually exist, it calls an error on the player3:CharacterRemoving. Is there a way I could have the :CharacterRemoving event happen if for example

players = {} -- This would have a different amount of players in each time
players:CharacterRemoving:Connect .... blah

--Then this would get called whenever one of the players inside the table dies?

Sorry, I feel like its a bit confusing, it’s hard to explain, let me know if you need any more details, thanks for any help. :slight_smile:

2 Likes

Wouldn’t this work the same way:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterRemoving:Connect(function(character)
		-- CODE
	end)
end)

If this helped mark it as a solution so people know it’s been solved.

3 Likes

No because I only want the character removing event to happen to happen to the players that are inside the arena, I don’t want it to happen to everyone in the server.

1 Like

Just do an if statement then:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterRemoving:Connect(function(character)
		if InArena[player.Name] then -- Something like this
           -- CODE
        end
	end)
end)
2 Likes

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