Script does not remove player from table when touching part

I reproduced the code in an empty world and it gave the same result.
But I added a wait() to it and it worked, something is missing to call the code.

no wait…
if the touched event returns a plr
but the one that touch is a leg
why does the _G saves a plr but it ends up as a leg…

you got the plr name but the one that touched was a leg not a plr
so therefore the name will never be a player name which means the if statement wont run

I don’t mean the part, I mean the code that should save the player names in the table.

Players = game:GetService("Players")
_G.players = {}
wait()
for i, player in pairs(Players:GetPlayers()) do
    print(player.Name)
    table.insert(_G.players,player.Name)
    print(_G.players)
end

If I remove the wait(), it prints the empty table.

Yeah ok
but i meant to reply to the topic owner

Are you sure? It is still not working for me. I used the script that you pasted in.

how bout mine???

hm which one are u talking about

uh
your touched event returns a leg but u thought it was a plr so therefore it would never be a players name
so change it to plr.Parent lol

https://gyazo.com/cbcdabb1ba649064420257004b89cd59

The map only has one SpawnLocation, the part, and the two scripts.

script.Parent.Touched:Connect(function(handsomeleg)
if handsomeleg.Parent:FindFirstChild("Humanoid") then
plr = handsomeleg.Parent
	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
end)
2 Likes

Can you send the full script you used?

i dont really know what is the issue, i think its ur way of iterating
i rescripted it with some comments and it seems to work

script.Parent.Touched:Connect(function(part)
	local plr=game.Players:GetPlayerFromCharacter(part.Parent);
	if plr then
		print("Touched!")
		print(_G.players)
		for i,v in pairs(_G.players) do
			if i==plr.UserId then -- i prefer using the userid instead of the name because it can change
				print(plr.Name .. " has died!")
				_G.players[plr.UserId]=nil; -- remove the player from the players table
				print(_G.players)
			end
		end
	end;
end)

the other script

Players = game:GetService("Players")

_G.players={};

game.Players.PlayerAdded:Wait(); -- u can remove it i put that just for testing

for i, player in ipairs(Players:GetPlayers()) do -- i personally use ipairs when i want to loop through arrays

print(player.Name)

_G.players[player.UserId]=true; -- i prefer using the userid instead of the name because it can change

end

Script (G):

Players = game:GetService("Players")
_G.players = {}
wait()
for i, player in pairs(Players:GetPlayers()) do
    print(player.Name)
    table.insert(_G.players,player.Name)
    print(_G.players)
end

Part:

script.Parent.Touched:Connect(function(plr)
    print("Touched!")
    print(_G.players)
    for i,PlayerName in pairs(_G.players) do
	    if PlayerName == plr.Name then
		    print(plr.Name .. " has died!")
		    print(_G.players)
	    	table.remove(_G.players,i)
    	end
	end
end)

image

It doesn’t remove it because it touches the foot like @hestolemyrice said.

script.Parent.Touched:Connect(function(handsomeleg)
if handsomeleg.Parent:FindFirstChild("Humanoid") then
plr = handsomeleg.Parent
	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
end)

You can store the player instance (and it’s also faster to access than the player’s name).

Here’s one way you can do it:

local Players = {}

if Players[Player] then return end
Players[Player] = true -- Add it to the table (dictionary, with a value of 'true').
-- Code.
Players[Player] = nil -- Remove it from the table.
1 Like

For some reason it won’t remove the player from the tableimage

P.S. It’s printing the die string normally.

not my script bro yeyeyeyeyeyeyeyeyeyeyeyeyeyeyyeye

1 Like

ok its because u put the print before removing the table element yay

everyone praise this guy for solving it uwu