What do you want to achieve? I want a player to get removed from a table when they die.
What is the issue? I can’t get the script to work.
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)
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.
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
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)
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