Removing a player from table when dead

  1. What do you want to achieve? I want a player to get removed from a table when they die.

  2. What is the issue? I can’t get the script to work.

  3. What solutions have you tried so far? I searched everywhere but solutions posted there wouldn’t work.

Here is the script:

game:GetService("Players").PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function(playerdied)
			print(player.Name .. " has died!")
			for i, player in pairs (game.Players:GetPlayers()) do
				warn("Player has been removed from the table.")
				for i = 1, #players do
					if players[i] == player then
						table.remove(players, i)
					end
				end
			end
		end)
	end)
  end)
2 Likes

Whoops, it seems like you forgot to put one more end) at the last line of the script. If that does not help you solve the problem, it’d be better for you to post the whole chunk of the script including variables used in the function you posted.

1 Like

I just forgot to select it when copying/pasting the script, haha.

1 Like

Would you post a whole script relating to your problem? And it would be even better if you add detailed explanation on your issue.

1 Like
players = {}
Players = game:GetService("Players")
for i, player in pairs(Players:GetPlayers()) do
	print(player.Name)
	table.insert(players, #players + 1, player.Name)
end
game:GetService("Players").PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function(playerdied)
			print(player.Name .. " has died!")
			for i, player in pairs (game.Players:GetPlayers()) do
				warn("Player has been removed from the table.")
				for i = 1, #players do
					if players[i] == player then
						table.remove(players, i)
					end
				end
			end
		end)
	end)
	
end)
repeat wait() until script.Parent.LoadedIn.Value == game.Workspace.Stage.Value


for _,player in pairs(Players:GetPlayers()) do
	player.PlayerGui.ScreenGui.Details.Text = "Stage 1 - Randomizer"
end
wait(4)
for _,player in pairs(Players:GetPlayers()) do
	player.PlayerGui.ScreenGui.Details.Text = "Every 5 seconds, a platform will dissapear!"
end
wait(4)
for _,player in pairs(Players:GetPlayers()) do
	player.PlayerGui.ScreenGui.Details.Text = "Be the last one standing!"
end

repeat -- This part is incomplete. Ignore if it's shooting an error.
	wait(100)
for _,player in pairs(Players:GetPlayers()) do
	player.PlayerGui.ScreenGui.Details.Text = ""
end
until #players == #players - 3

Why are you looping through all players? just table.remove(players, player) after the humanoid dies

1 Like

Nevermind I forgot table.remove requires the table number, instead you can do:

local PlrTableId = table.find(Players, Player)

table.remove(Players, PlrTableId)
1 Like

If the script is running from the start of the game, you’re trying to loop through the empty player list and add it at the beginning.

So instead of placing

for i, player in pairs(Players:GetPlayers()) do
	print(player.Name)
	table.insert(players, #players + 1, player.Name)
end

at the beginning, you can place table.insert(players, #players + 1, player.Name) with CharacterAdded event, which will look like:

game:GetService("Players").PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
        table.insert(players, #players + 1, player.Name)
		character:WaitForChild("Humanoid").Died:Connect(function(playerdied)
			print(player.Name .. " has died!")
			for i, player in pairs (game.Players:GetPlayers()) do
				warn("Player has been removed from the table.")
				for i = 1, #players do
					if players[i] == player then
						table.remove(players, i)
					end
				end
			end
		end)
	end)
end)
1 Like

image

Could you clip the 21th line of the script?

Perhaps I should start the script all over and explain better what I am trying to achieve:

When a BoolValue in the workspace is set to true, the dead removal script starts running and when set to false, it stops running.

Seems like I misunderstood from the beginning, apologies.

One thing is that I still don’t get what does “I can’t get the script to work.” means, meanwhile your script looks seamless.

I meant that when a player dies, there is no print() indicator that shows that they got removed from the table whatsoever.

Probably that’s because of you’re saving player.Name which is string value in the table, but you’re looping through table with if statement which finds for the player instance.

game:GetService("Players").PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function(playerdied)
			print(player.Name .. " has died!")
			for i, player in pairs (game.Players:GetPlayers()) do
				warn("Player has been removed from the table.")
				for i = 1, #players do
					if players[i] == player.Name then -- Previously 'if players[i] == player then'
						table.remove(players, i)
					end
				end
			end
		end)
	end)
end)

If your table is for example:
{ game.Players.Player1, game.Players.Player2 }
you could just do:
table.remove( players, table.find( players, game.Players.Player1 ) )
and it should work fine assuming that I understood your question

1 Like