What is wrong with my script?

Hello,

So I am trying to script a system where if you click a button then it willl add u to a table and a UI baord thing but when you leave if you are on the board you get taken off and removed from the table. I have not worked with tables much and for some reason it will not remove the player.

I am also geting another issue where it will not remove the user from the board (I get an error for this but not the table removeal one)

Script:

– Add player on host board-

local Players = {}

game.ReplicatedStorage.HostStuff.AddPlayer.OnServerEvent:Connect(function(plr)
	print("4454535")
	if plr:GetRankInGroup(6559630) >= 20 then
		print("ffffff")
		if not Players[plr]  then
			print("Player is not in host system but now is.")
			Players[plr] = true
		else
			print("Player is already in the host system")
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	if plr:GetRankInGroup(6559630) >= 20 then
		if Players[plr.Name]  then
			print("Player who left is on the list")
			if game.ServerStorage.CurrentHost.Value == plr.Name then
				print("Host")
			else
				for i, v in pairs(game.Players:GetChildren()) do
					if not plr then
						v.PlayerGui.SurfaceGui.ListDown:FindFirstChild(plr.Name):Destroy()
					end
				end
				Players[plr.Name] = false
				print(Players)
			end
		end
	end
end)

Error I get:

You should probably do v:WaitForChild("PlayerGui").

I don’t think that would change anything cuz the UI is already there so it would just be waiting for nothing.

Also that would not affect the table issue with it not removing the player from the table.

				for i, v in pairs(game.Players:GetChildren()) do
					if not plr then
						v.PlayerGui.SurfaceGui.ListDown:FindFirstChild(plr.Name):Destroy()
					end
				end

You need to change the code above to this:

				for i, v in pairs(game.Players:GetChildren()) do
					if v ~= plr then
						v.PlayerGui.SurfaceGui.ListDown:FindFirstChild(plr.Name):Destroy()
					end
				end

plr will always be true in your case because v is the index (which iterates through all the player values) so if you want to check if the player in the loop does not equal to the designated player (in this case plr) you need to check if v ~= plr

Any got any idea why it will not remove the player from the table? Is it because of that error it does not run the Players[plr.Name] == false?

			Players[plr.Name] = false

instead of this, do Players[plr.Name] = nil