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

How to remove a player from a table?

How can I check if a player is in a table and if so remove them, how can I do this without knowing their position in the table?


game.ReplicatedStorage.WaitingTeam.OnServerEvent:Connect(function(plr)
local piwt = {}
local pig = {}
table.insert(piwt,plr)
if #piwt == 1 then
	for i, v in pairs(piwt) do
	     table.insert(pig,v)
	end
	for i, v in pairs(pig) do
		if v == plr.Name then
			game.ReplicatedStorage.GameBegins:FireClient()
		end
	end
	
end
game.Players.PlayerRemoving:Connect(function(p)
	table.remove(piwt,--p)
end)

2 answers

✓ Accepted answer

You just have to iterate through the table and you will use the index of the entry to remove that value.

For example:

game.Players.PlayerRemoving:Connect(function(p)
	for i, v in pairs(pigwt) do
		if v == p
			table.remove(pigwt, i)
		end
	end
end)

4 comments

0

I'm not seriously!! I just upvoted you I don't get it?

0

Would you mind removing your downvote?

0

That isn't me, I didnt downvote you, i'll uppvote your answer though

0

Huh, this is getting crazy

You can use the table.find() function to locate the index placement of the element passed via the secondary argument. Since table.remove() takes an index argument of the element you wish to eradicate, we can use these two in conjunction if you're absolutely sure they exist in the array:

table.remove(pwit, table.find(pwit, p))

Otherwise, you can iterate through the contents of the array you wish to remove an element from, and compare each traversed value to the object you wish to locate for verification:

for Placement,_ in pairs(pwit) do
	if (table[Placement] == p) then
		table.remove(pwit, Placement)
	end
end

5 comments

0

Lol you beat me

0

Bro you gotta stop downvoting me for no reason

0

So there's someone just downvoting both of us randomly?

0

I just upvoted you... my reputation didn't change either

0

I really don't know?

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