Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do i delete a table array after a players leaves the game with the same name?

if a player leaves (for example Player2), how do i make the table delete the array "Player2" after Player 2 leaves from the game?

i'm baffled and this is all i could do right now:

players = {}

local function onCharacterAdded(chr)
	table.insert(players, chr)
	local value = players[math.random(1,#players)]
	print(value)
end

local function onCharacterRemoved(removed)
	table.remove(players, removed)
end

game.Players.PlayerAdded:Connect(onCharacterAdded)
game.Players.PlayerRemoving:Connect(onCharacterRemoved)

while true do
	wait(2)
	print(players[1])
	print(players[2])
end

1 answer

✓ Accepted answer

table.remove only works if the index value is a number and not an instance type. So for this to work you'd have to find the index of that specific player and then use table.remove to remove the index in the table.

local index = -1

for i, v in pairs(players) do
	if removed == v then
		index = i
	end
end

table.remove(players, index)

This should find the index of the player removing and then remove that player from the table.

This discussion is preserved as it was. New answers, comments, and votes are closed.