At the time that you assign the allplayers variable, it's possible that not all the players have loaded in the game. You need to set the variable right before you need to use it so you can get the most accurate list of players.
Also, you can't just add .Character to the end of a list of players. You'd have to use a for loop to iterate through each player in order to move all of them to the map.
local maps = game.ReplicatedStorage:WaitForChild("Maps"):GetChildren()
_G.playersAlive = {}
while true do
_G.playersAlive = {}
game.ReplicatedStorage.status.Value = "Intermission (20 Seconds)"
wait(18)
game.ReplicatedStorage.status.Value = "Selecting map, Hold On!"
wait(2)
game.ReplicatedStorage.Maps.Map1:Clone().Parent = game.Workspace
game.ReplicatedStorage.status.Value = "Map selected: Lab"
wait(1)
local allspawns = game.Workspace.Map1.Spawns:GetChildren()
local allPlayers = game.Players:GetPlayers()
for i, v in pairs(allPlayers) do
local character = v.Character
if character then
local humanoid = character:FindFirstChild("Humanoid")
if humanoid then
character:MoveTo(allspawns[i].Position)
table.insert(_G.playersAlive, v)
end
end
end
wait (2)
game.ReplicatedStorage.status.Value = "Get Ready!"
wait(5)
game.ReplicatedStorage.status.Value = "Go!"
for i, v in pairs(_G.playersAlive) do
local backpack = v:WaitForChild("Backpack")
local knife = game.ReplicatedStorage:WaitForChild("Knife"):Clone()
knife.Parent = v.Backpack
local revolver = game.ReplicatedStorage:WaitForChild("Revolver"):Clone()
revolver.Parent = v.Backpack
end
wait (180)
for i, v in pairs(alivePlayers) do
local chr = v.Character
if chr then
local humanoid = chr:FindFirstChild("Humanoid")
if humanoid then
humanoid.Health = 0
end
end
end
wait(8)
game.Workspace.Map1:Destroy()
end
I added in the for loops, added some WaitForChild functions on some stuff(because you can't ever assume that an instance has loaded), and I also added a global table that keeps track of the players that are currently in the game. The reason I did this was because if someone dies you should know. And the reason I made the table a global variable(meaning it can be accessed from other scripts) is because I want to recommend putting this script in your game:
game.Workspace.ChildAdded:Connect(function(child)
local plr = game.Players:GetPlayerFromCharacter(child)
if plr then
local humanoid = child:WaitForChild("Humanoid")
humanoid.Died:Connect(function()
if table.find(_G.alivePlayers, plr) then
table.remove(_G.alivePlayers, table.find(_G.alivePlayers))
end
end)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local index = table.find(_G.alivePlayers, plr)
if index then
table.remove(_G.alivePlayers, index)
end
end)
It listens for when a child is added to the workspace, and determines whether or not that child is the character of a player. If it is, listen for when its humanoid dies, and when it does, if it is inside of the _G.alivePlayers global table, then remove the player them from it. It also accounts for players leaving the game.
I know this is much more than you requested, but I seriously couldn't help myself. I saw that you weren't keeping track of the players and just couldn't ignore that.
4 comments
Thank su
You're setting the allPlayers variable once, at the beginning of the game. What if in the 20 seconds before the map is chosen more people join or leave? You still won't have the updated version of the allPlayers variable. You use the same random spawn for every player? They'd all teleport to the same spawn at the same time. May be a bit of a strange experience.
You're assuming that their Character and HumanoidRootPart have loaded on line 16. You also put ".Backpack" after the allPlayers variable... despite the fact that it is a table and you'd have to iterate through the table to get each individual player's backpack.
Seriously, this is rushed like crazy. Take your time and don't rush answers.