Unknown bug related to removing values from a table

  • What are you attempting to achieve? (Keep it simple and clear)
  • What is the issue? (Keep it simple and clear - Include screenshots/videos/GIFs if possible)
  • What solutions have you tried so far? (Have you searched for solutions through the Roblox Wiki yet?)

What I’m trying to achieve, is to simply, remove a value from the table. The table is, called “PlayersConnected” and contains 4 other tables within the table, at the start of the script being set up as:

local PlayersConnected = {{},{},{},{}}

When I run the function to remove the table, I’ve used debugging to try find the error:

local function RemovePlayer(player)
local t = ""
for _, k in pairs(PlayersConnected) do
	t = t .. " | "
	for i, v in pairs(k) do
		t = t .. tostring(v)
	end
end
print("Before: " .. t)
for i, tab in pairs(PlayersConnected) do
	for j, v in pairs(tab) do
		if v == player then
			table.remove(PlayersConnected[i],j)
		elseif not Players:FindFirstChild(v.Name) then
			table.remove(PlayersConnected[i],j)
		end
	end
end
t = ""
for _, k in pairs(PlayersConnected) do
	t = t .. " | "
	for i, v in pairs(k) do
		t = t .. tostring(v)
	end
end
print("After: " .. t)

end

Sorry that this is phrased a bit all over the place, I’ll explain the actual error here and what’s meant to happen. When a player leaves a pad, the name is removed from the list, which works fine with:

if v == player then
	table.remove(PlayersConnected[i],j)
elseif not Players:FindFirstChild(v.Name) then
	table.remove(PlayersConnected[i],j)
end

The second elseif (i’m aware you can use “or”, swapped for debugging) searches through Players to see if the player is still in the game, and if they aren’t, to run that code section. I’ve confirmed that, if a player isn’t in the game, that section of the if statement runs. I’ve also confirmed that the other if statement (if v == player) runs AND removes the value from the table. However, by printing the full table before and after the function runs, I’ve confirmed that, for some reason, the value isn’t being removed from the table when the player isnt in the server.

I’ve tried a multitude of solutions across 2 -3 days, from changing the script entirely, checking if its updating with print statements, changing it to Player.Name instead of Player for everything related to it, but in my eyes, logically, this piece of code should work if the if statement before it works?

Thanks for reading, if you can’t understand anything/need more details, please ask.

From my understanding when you add objects to a table, like physical instances for say, and when their physical instance is removed from the workspace, their data is still linked to the variable or whatever it might be.

I’ve run into this several times, and turns out Roblox doesn’t handle this automatically. I wanna say this is related to garbage collection but I could be totally wrong on that.

As far as solutions go, you could simply create a PlayerRemoving event and search through your table to see if that player is found, and if he is, you could set that to nil to completely remove of it.

Hope this helps.

I’m already doing this with a PlayerRemoving event.

local function Remove(plr)
   if plr.Parent.Name ~= "Players" then plr = plr.Parent end 
   RemovePlayer(Players:FindFirstChild(plr))
   UpdateInfo()
end
Players.PlayerRemoving:connect(function(Player)
 	Remove(Player)
end)

I’ll change it to FindFirstChild(plr.Name) and see if that makes a difference

Are you getting any error when you run this? I would think the if statement at the top of your “Remove” function is unnecessary, based on speculation. Just curious if that’s throwing an error causing that function to stop executing

I’m not getting any error, and it seems as if changing it to plr.Name has fixed it in a local server, I’ll try it in team test once my colleague gets back, though I honestly don’t understand that seeing as that was what it was earlier on, though I changed it in an attempt to fix it.

1 Like

Oh, something just caught my eye as I read your comment. The method FindFirstChild only takes string data type as an argument, and I noticed that you were attempting to pass in the actual player being the argument (or it’s parent), which I assume is why nothing happened at that point.

1 Like

Yeah, I realised after I sent the reply to yours with the code paste that I was sending through the actual player, which is why I tried to change it - it worked in a local server 2 player test, though my colleague isn’t able to be here to test team test, so I’ll re-test another 2 player local server and if it works again, I’ll consider that the solution.

In regards to “or it’s parent”, plr = plr.Parent is for the TouchEnded event, i.e. its more like limb = limb.Parent, I’m just using the same function for both events. :stuck_out_tongue:

1 Like

Yeah, it worked, thanks.

In the end, 3 days of killing my brain boiled down to “.Name”. Guess I’m a fail of a scripter xP

2 Likes