Script does not remove player from table when touching part

title

Here is the script:

script.Parent.Touched:Connect(function(plr)
	print("Touched!")
	print(_G.players)
	for i = 1, #_G.players do
		if _G.players[i] == plr.Name then
			print(plr.Name .. " has died!")
			print(_G.players)
		end
	end
end)

I am not getting any errors in the output.

can i see the varable of _G.players

yesh please show the table so we understand em

It’s a table where player names get stored every round. In the script it’s just

_G.players = {}

This is the block of code that stores the table elements each round:

Players = game:GetService("Players")

for i, player in pairs(Players:GetPlayers()) do

print(player.Name)

table.insert(_G.players, #_G.players + 1, player.Name)

end

use table.remove(tableName, index)

i think you havnt remove the player from the table here

for i = 1, #_G.players do
	if _G.players[i] == plr.Name then
		print(plr.Name .. " has died!")
		print(_G.players)
         local found = table.find(_G.players,plr.name)
        table.remove(_G.players, found)
	end
end

Hey guys thanks for demonstrating that I have been scripting for the last 8 hours and I am feeling tired imma grab some coffee wait.

Hey guys update it doesn’t work.

What doesn’t work? show us if there is errors

There are no errors in the output and the table nor the string is outputing:
Expected result:
Kostiskat has died!
{} (Empty table because player was removed from it)

Where and what is this script?

Perhaps I should probably mention that. The script is inside a touched event:

script.Parent.Touched:Connect(function(plr)
	for i = 1, #_G.players do
		if _G.players[i] == plr.Name then
			print(plr.Name .. " has died!")
			print(_G.players)
			local found = table.find(_G.players,plr.name)
			table.remove(_G.players, found)
		end
	end
end)

maybe add a break after it because it shouldnt print nothing:

script.Parent.Touched:Connect(function(plr)
	for i = 1, #_G.players do
		if _G.players[i] == plr.Name then
			print(plr.Name .. " has died!")
			print(_G.players)
			local found = table.find(_G.players,plr.name)
			table.remove(_G.players, found)
            break
		end
	end
end)

In the game did u die or was this script just a test an the “died” is fake?

The part is supposed to teleport you to the lobby without killing you, as it makes it easier for me to just remove them from the table easily. (The teleport block of code is not intergrated yet)

no no no
Dont add a break because if it only for loop once and breaks it might not looped till the plr yet and the script go not working

actually its an if statement so when it found the player then it will execute the code and then break it afterward

Ok dont flag this
uh is this part of ur script or is it ur whole script

2 Likes

Maybe try:

script.Parent.Touched:Connect(function(plr)
    if table.find(_G.players, plr.Name) then
       print(plr.Name.." has died!")
       table.remove(_G.players, table.find(_G.players, plr.Name))
    end
 end)

This is the whole script. The parent is just a transparent block.