This script is for a game i'm making and it works up until the respawning all script. I don't believe there are any errors. If you could tell me why it is working I would really appreciate it. It is a script in script server service.
MoreThan2PlayersValue = game.ReplicatedStorage.MoreThan2PlayersYN if MoreThan2PlayersValue.Value == true then players = game:GetService("Players"):GetPlayers() randomPlayer = players[math.random(1, #players)] print("picked a random player") end for _,Player in pairs(game.Players:GetPlayers()) do --Looping through all players Player.TeamColor = BrickColor.new("Bright yellow") print("teamed all baghead") end TheSurvivor = randomPlayer game.Players.TheSurvivor.TeamColor = BrickColor.new("Bright green") print("teamed the survivor") for i,v in ipairs(game.Players:GetPlayers()) do v:LoadCharacter() end print("attempted to respawn all players!") while wait(1) do if game.TheSurvivor.Humaniod.Health <= 0 then for _,Player in pairs(game.Players:GetPlayers()) do --Looping through all players Player.TeamColor = BrickColor.new("Bright blue") end game.ServerStorage.StartAfter2PlayersJoin:Clone() game.ServerStorage.StartAfter2PlayersJoin.Parent = game.ServerScriptService script.Parent:Destroy() end end
(It makes it to the respawn part so I only need help on that part, I included the rest of the script just in case something in there is relevant to it not working.)
As bronzedmocha said, change ipairs
to pairs
.
In line 16-17, you need to use square brackets to index a variable.
local TheSurvivor = randomPlayer game:GetService('Players')[TheSurvivor.Name]
You should use DataModel:GetService( Instance className )
to get services, rather than game.Service
.
Instead of using game.Players
, use game:GetService("Players")
.
Edit = You mentioned your script was a script in ServerScriptService
, and script.Parent
would be a reference to the service, and on line 33 you called the Instance:Destroy()
method on the ServerScriptService
. Services cannot be destroyed.