Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I make it so that when a player dies, their name is written off a table?

Here's my script:

_G.addremovePlayers = function()
	for _,v in pairs(game.Players:GetChildren()) do
		table.insert(_G.playersLeft,v.Character.Name)
	end
	for i = 1,#_G.playersLeft do
	print(_G.playersLeft[i])
	end
end

I can't really seem to figure out how to remove someone's name from a table when they die.

Let me be more specific if you don't understand. 3 players join a game and when the round starts, their names are put in a table. {a, b, c}. Player b dies and I want b to be taken out of the table so it would be {a, c}.

2 comments

0

I bet you didn't even read it properly.

3 answers

if you put this in the StarterCharacterScripts, I believe it should work:

local humanoid = script.Parent:FindFirstChild("Humanoid")
humanoid.Died:Connect(function()
	table.remove(_G.playersLeft,script.Parent.Name)
end)

3 comments

0

:thinking: alright I'll try it out

0

uh oh, looks like it didn't work!

0

does it work fine without this part?

Hi.

I never actually experimented with this, soo.. Let's give it a go.

function participate()
	for _, Player in pairs(game.Players:GetPlayers()) do
		_G.playersLeft[Player.Name] = true
		v.Character:FindFirstChildOfClass("Humanoid").Died:connect(function()
			print(Player.Name.. ' Has died! Remaining players: '.. #_G.playersLeft)
			_G.playersLeft[Player.Name] = nil
		end)
	end
end

game:GetService('ReplicatedStorage').RemoteFunctions.addPlayers.OnServerInvoke = participate

If it errors, let me know.

1 comment

0

Oh yeah, I assumed there was a remotefunction named addPlayers, also. If that's not a thing, you might want to create it. kek

One way to do it is to iterate through each value in that table, then get the name of the player who dies, and remove it if found.

Example: (Note you have to replace and change some stuff in the code below)

humanoid.Died:Connect(function()
for i,v in pairs(_G.playersLeft) do
if v.Name == PLAYERNAME then
table.remove(_G.playersLeft, i)
end
end
end

This discussion is preserved as it was. New answers, comments, and votes are closed.