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

How would I remove a value from a table?

So i'm trying to make a script that detects when a player enters & leaves by putting their name in a table. Problem is i'm not sure if you can use table.remove() to remove a value,

	local players = {}
game.Players.ChildAdded:connect(function(plr)
	table.insert(players, ""..plr.Name)
end)

game.Players.ChildRemoved:Connect(function(plr)
	table.remove(players, ""..plr.Name)
end)

Any tips on how I can get it to remove the child's name off of the table when they leave?

1 answer

✓ Accepted answer

You can't remove an item from a table by its name, you can only do it by its position. What you could do is cycle through the table until you find the position that matches the player's name, and remove that position.

Here is what I mean:

local players = {}

game.Players.ChildAdded:connect(function(plr)
	table.insert(players, ""..plr.Name)
end)

game.Players.ChildRemoved:Connect(function(plr)
	table.remove(players, ""..plr.Name)
	for i=1, #players do
		if plr.Name == players[i] then
			table.remove(players, i)
		end
	end
end)

1 comment

0

Use PlayerAdded in the future.

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