I’ve been trying to get this script to work for a while, but it just doesn’t work. I’m trying to make a zombie game in which when a player dies, they are inserted into an aliveplrs table, and when eliminated are put into a deadplrs table. However, for some reason, the player isn’t being inserted into the table when I want him to.
When I have a respawn wave where all players that were killed are respawned, the player comes back. However, he is not inserted into the deadplrs table, making it so if anyone who was alive before dies, the game ends regardless of how many people there are still left in the game
Here is the code for inserting people into tables. I can send the entire main script as well if you would like.
for i, plr in pairs(game.Players:GetPlayers()) do
-- plr:LoadCharacter()
table.insert(alivePlrs, plr)
plr.CameraMaxZoomDistance = 0.5
plr.CameraMinZoomDistance = 0.5
local RemoteCamEvent = replicatedstorage:WaitForChild("ChangeCameraCustom")
RemoteCamEvent:FireClient(plr)
plr.PlayerGui.Menu.Enabled = false
plr.Character.Humanoid.Died:Connect(function()
table.remove(alivePlrs, alivePlrs[plr])
table.insert(deadplrs, plr)
plr.CameraMaxZoomDistance = 50
print("Player has been added to the dead players table")
--plr.PlayerGui.Menu.Enabled = true
end)
--gun:Clone().Parent = plr.Backpack
end
And here is the code for respawning players
for i, player in pairs(deadplrs) do
if player then
character = player.Character
if character then
-- teleport them
local RemoteCamEvent = replicatedstorage:WaitForChild("ChangeCameraCustom")
RemoteCamEvent:FireClient(player)
wait(1)
player.CameraMaxZoomDistance = 0.5
player.CameraMinZoomDistance = 0.5
player.PlayerGui.Menu.Enabled = false
table.insert(alivePlrs, deadplrs[player])
print("Added to alive players table")
character:FindFirstChild("HumanoidRootPart").CFrame = availablespawnpoints[1].CFrame + Vector3.new(0, 10, 0)
-- player.PlayerGui.Menu.Enabled = false
table.remove(availablespawnpoints, 1)
--give sword
local equipped = game.ServerStorage.PlayerData[player.Name].Equipped
if equipped.Value ~= "" then
local weapon = game.ServerStorage.Items[equipped.Value]:Clone()
weapon.Parent = player.Backpack
else
local gun = serverstorage.M9:Clone()
gun.Parent = player.Backpack
end
else
-- no character found
if not player then
table.remove(alivePlrs, i)
end
end
end
end
The problem is in the respawn script when trying to insert the player back into the aliveplrs table. “table.insert(alivePlrs, deadPlrs[player])”. It doesn’t insert the player back into the table.