Hello, I am making a last man standing game, and I need to remove the “players” from a table. The issue is the players are Instances so it won’t let me. Here is the main code:
local matchPlayers = {}
for i, v in pairs(Players:GetPlayers()) do
table.insert(matchPlayers, v)
end
while true do
wait()
for i, v in pairs(matchPlayers) do
v.Character.Humanoid.Died:Connect(function()
table.remove(matchPlayers, v)
end)
Players.PlayerRemoving:Connect(function(v)
table.remove(matchPlayers, v)
end)
end
end
And here is my full code:
wait(1)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local function restart()
script.Disabled = true
wait()
script.Disabled = false
end
local maps = {
ReplicatedStorage.ForestMap,
ReplicatedStorage.WorshipMap,
ReplicatedStorage.NeonMap
}
local gameStatus = ReplicatedStorage.gameStatus
while true do
--Waiting
gameStatus.Value = "Waiting for Players"
--repeat wait() until Players.NumPlayers > 1
--Intermission
gameStatus.Value = "Intermission, Time Left: "
local timeLeft = 15
for i = 1, timeLeft do
gameStatus.Value = ("Intermission, Time Left: " .. timeLeft)
wait(1)
timeLeft -= 1
end
--StartingGame
local chosenMap = maps[math.random(1, #maps)]
chosenMap.Parent = game.Workspace
local matchPlayers = {}
for i, v in pairs(Players:GetPlayers()) do
table.insert(matchPlayers, v)
end
print(#matchPlayers)
gameStatus.Value = "Game In Progress"
for i, v in pairs(matchPlayers) do
v.Character:MoveTo(Vector3.new(math.random(-30, 30), 30, math.random(-30, 30)))
local sword = ReplicatedStorage["Wooden Sword"]:Clone()
sword.Parent = v.Backpack
end
--Game
while true do
wait()
for i, v in pairs(matchPlayers) do
v.Character.Humanoid.Died:Connect(function()
table.remove(matchPlayers, v)
end)
Players.PlayerRemoving:Connect(function(v)
table.remove(matchPlayers, v)
end)
end
end
--WinnerRewardAndGameEnd
if #matchPlayers == 1 then
print("game ended")
print(matchPlayers[1])
matchPlayers[1].leaderstats.Wins.Value += 1
print(matchPlayers[1].leaderstats.Wins.Value)
matchPlayers[1].Character.Humanoid.Health = 0
chosenMap.Parent = ReplicatedStorage
restart()
end
end