Script does not remove player from table when touching part

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

so we spent an hour trying to figure out the problem only to realised you thought a leg was a player
:clap:
give my leg a name now

legs are op! yayyyyy (3ocharrrr)

uwu
:bowing_woman: :bowing_woman: :bowing_woman: :bowing_woman:
xd

im sry but no i got it first

???
ok i need to sleep because i realised im going to get mod 6-10 times

1 Like

Why are you using _G in the first place?
_G is used to share data between scripts, in this case, you are not doing that.

I suggest switching over to a local variable.
And, if you ever need to share data between scripts, a ModuleScript works fine for that.

The case is closed but the 1st script is located in the part and the 2nd is in SSS

Then a ModuleScript works just fine for that.
Here’s why.

That girl was using a 10k line script. I planning on using just 1 _G. I don’t think the game’s performance is depending on 1 global table.