Game Loop Script Doesn't Work

Hello! I’m trying to make a disaster game with zCrxtix, the original game was in the crossroads map were litterly a red pole that would cause a giant explosion.

Anyway, My Game Loop Script wont work, I’m trying to make it so that the player’s name will go to the Survivors List if they survived in the game.

Error

  16:16:10.390  ServerScriptService.GameLoop:124: Expected ',' when parsing index range, got 'in'  -  Studio - GameLoop:124
  16:16:21.979  0.5, 0.5  -  Server
  16:16:22.263  0.5, 0.5  -  Client

Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local ServerStorage = game:GetService("ServerStorage")

local MapsFolder = ServerStorage:WaitForChild("Maps")

local GameStatisticsGui = game.StarterGui.GameStatisticsGui

local SurvivorList = GameStatisticsGui:WaitForChild("SurvivorsFrame")

local WinReward = 1

local CoinReward = 25

local Status = ReplicatedStorage:WaitForChild("Values"):WaitForChild("Status")

local GameLength = 120

-- Game Loop

while true do
	
	Status.Value = "Waiting For 2 Players..."
	
	repeat wait() until game.Players.NumPlayers >= 1
	
	Status.Value = "Intermission..."
	
	wait(20)
	
	local plrs = {}
	
	for i, player in pairs(game.Players:GetPlayers()) do
		if player then
			table.insert(plrs,player) -- Adding Players
		end
	end
	
	wait(2)
	
	local AvaliableMaps = MapsFolder:GetChildren()
	
	local ChosenMap = AvaliableMaps[math.random(1, #AvaliableMaps)]
	
	Status.Value = "Map Chosen!"
	
	local ClonedMap = ChosenMap:Clone()
	
	ClonedMap.Parent = workspace
	
	-- Telaport Map
	
	local SpawnPoints = ClonedMap:FindFirstChild("SpawnPoints")
	
	if not SpawnPoints then
		warn("SpawnPoints Can't Be Found! Check if The Grouped Map Has Them!")
	end
	
	local AvaliableSpawnPoints = SpawnPoints:GetChildren()
	
	for i, player in pairs(plrs) do
		if player then
			character = player.Character
			
			if character then
				-- Telaport Them
				
				character:FindFirstChild("HumanoidRootPart").CFrame = AvaliableSpawnPoints[1].CFrame
				table.remove(AvaliableSpawnPoints,1)
				
				
				-- Game Tag
				local GameTag = Instance.new("BoolValue")
				GameTag.Name = "GameTag"
				GameTag.Parent = player.Character
			else
				warn("Player Can't Be Found!")
				
				if not player then
					table.remove(plrs,i)
				end
			end
		end
	end
	
	wait(2)
	
	Status.Value = "The Game Is Starting!"
	
	for i = GameLength,0,-1 do
		for x, player in pairs(plrs) do
			if player then
				
				
				character = player.Character
				
				if not character then
					-- Player Left
				else
					if character:FindFirstChild("GameTag") then
						-- Their Alive
					else
						-- Their Dead
						table.remove(plrs,x)
					end
				end
			else
				table.remove(plrs,x)
			end
		end
		
		Status.Value = "Time: "..i
		
		wait(1)
		
		if GameLength == 0 then
			for i, player in pairs(#plrs) do
				if player then
					for i, player in pairs(plrs) do
						if player then
							for i, GameStatisticsGui in pairs(plrs) do
								local GameStatisticsTextLabels = GameStatisticsGui.SurvivorsFrame:GetChildren()
								
								for i = 1 + plrs in pairs(#plrs) do
									GameStatisticsTextLabels[i] = plrs.Name
									if #plrs == 0 then
										Status.Value = "Nobody Won!"
									end
								end
							end
						end
					end
					character = player.Character

					if character then
						
					end
				end
			end
		end
		
		Status.Value = "End of game!"
		
		for i, player in pairs(game.Players:GetPlayers()) do
			character = player.Character
			
			if not character then
				-- Ignore
			else
				if character:FindFirstChild("GameTag") then
					character.GameTag:Destroy()
				end
			end

			player:LoadCharacter()
		end
	end
	
	ClonedMap:Destroy()
end

Explorer and GUI

Screenshot 2021-08-18 162146

also, How am I supposed to make the Survivor UI pop up for everyone?

Ok, First of all, please, when formatting the script in the DevForum, try to compact your script and make it easier to read. This helps everyone who looks at your scripts to not have to scroll 2 pages just to look at one line. Here is a more compact version of your script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local MapsFolder = ServerStorage:WaitForChild("Maps")
local GameStatisticsGui = game.StarterGui.GameStatisticsGui
local SurvivorList = GameStatisticsGui:WaitForChild("SurvivorsFrame")
local WinReward = 1
local CoinReward = 25
local Status = ReplicatedStorage:WaitForChild("Values"):WaitForChild("Status")
local GameLength = 120

-- Game Loop
while true do
	
	Status.Value = "Waiting For 2 Players..."
	repeat wait() until game.Players.NumPlayers >= 1
	Status.Value = "Intermission..."
	wait(20)

	local plrs = {}
	for i, player in pairs(game.Players:GetPlayers()) do
		if player then
			table.insert(plrs,player) -- Adding Players
		end
	end
	
	wait(2)
	
	local AvaliableMaps = MapsFolder:GetChildren()
	local ChosenMap = AvaliableMaps[math.random(1, #AvaliableMaps)]
	
	Status.Value = "Map Chosen!"
	
	local ClonedMap = ChosenMap:Clone()
	ClonedMap.Parent = workspace
	
	-- Teleport Map
	local SpawnPoints = ClonedMap:FindFirstChild("SpawnPoints")
	if not SpawnPoints then
		warn("SpawnPoints Can't Be Found! Check if The Grouped Map Has Them!")
	end
	
	local AvaliableSpawnPoints = SpawnPoints:GetChildren()
	
	for i, player in pairs(plrs) do
		if player then
			character = player.Character
			
			if character then
				-- Teleport Them
				
				character:FindFirstChild("HumanoidRootPart").CFrame = AvaliableSpawnPoints[1].CFrame
				table.remove(AvaliableSpawnPoints,1)
				
				-- Game Tag
				local GameTag = Instance.new("BoolValue")
				GameTag.Name = "GameTag"
				GameTag.Parent = player.Character
			else
				warn("Player Can't Be Found!")
				if not player then
					table.remove(plrs,i)
				end
			end
		end
	end

	wait(2)
	Status.Value = "The Game Is Starting!"
	
	for i = GameLength,0,-1 do
		for x, player in pairs(plrs) do
			if player then
				character = player.Character
				if not character then
					-- Player Left
				else
					if character:FindFirstChild("GameTag") then
						-- Their Alive
					else
						-- Their Dead
						table.remove(plrs,x)
					end
				end
			else
				table.remove(plrs,x)
			end
		end
		
		Status.Value = "Time: "..i
		wait(1)
		
		if GameLength == 0 then
			for i, player in pairs(#plrs) do
				if player then
					for i, player in pairs(plrs) do
						if player then
							for i, GameStatisticsGui in pairs(plrs) do
								local GameStatisticsTextLabels = GameStatisticsGui.SurvivorsFrame:GetChildren()
								
								for i = 1 + plrs in pairs(#plrs) do
									GameStatisticsTextLabels[i] = plrs.Name
									if #plrs == 0 then
										Status.Value = "Nobody Won!"
									end
								end
							end
						end
					end
					character = player.Character

					if character then
					end
				end
			end
		end
		
		Status.Value = "End of game!"
		
		for i, player in pairs(game.Players:GetPlayers()) do
			character = player.Character
			if not character then
				-- Ignore
			else
				if character:FindFirstChild("GameTag") then
					character.GameTag:Destroy()
				end
			end
			player:LoadCharacter()
		end
	end
	ClonedMap:Destroy()
end
1 Like

when you start maps up, id usually put the map into the workspace, once the game is done ill put it back into storage because I don’t think destroying an entire cloned map would be great unless you have to

if you want to show everyone the who survived ui, your gonna have to use events

there is definitely a better way to show all the survivors instead of manually placing the ui texts into the frame

and this “game tag” has another script that keep tracks of the player’s status of weather they are still in the game or not correct?

Now, the way you make UI pop up for everyone is to use a remote even and use this script:

--Server
local Event = game.ReplicatedStorage.ShowAll --Create new remote event in replicated storage and name it "ShowAll"
Event:FireAllClients()
--Server
local Event = game.ReplicatedStorage.ShowAll --Create new remote event in replicated storage and name it "ShowAll"
Event.OnClientEvent:Connect(function()
     --Show the gui code
end)
2 Likes

Yes, that is what its for. That’s why the game tag gets destroyed if the player leaves or dies

Instead of having so many TextLabels in the frame, delete them all and use a script to put them in.
I think the game loop is a server script, correct? So after the round is done, put this script. (Obviously change the variables to suit your needs.)

I suggest adding an in-game folder to workspace, when starting the game, put all the players’ characters into the folder.

local Players = game.Players:GetPlayers()
local InGame = workspace.InGame:GetChildren()
if #InGame < 1 then
    --Everyone Died code
else
     for i = 1,#Players do
           if Players[i].Character.Parent == InGame then
                 local Label = Instance.new("TextLabel")
                 Label.Parent = SurvivorsFrame
                 Label.Text = Players[i].Name
           end
     end
end

To help with formatting and changes to the textlabel (for example colors, and fonts and sizes) clone a textlabel already made instead of a new one. Do this just before killing everyone.

3 Likes

These Scripts Don’t Work, I’m trying to make a disaster game but the issue is that these scripts wont work at all, they just error.

Error

  12:24:20.318  RBLXDisasters @ 19 Aug 2021 12:24 auto-recovery file was created  -  Studio - C:/Users/mrtix/OneDrive/Documents/ROBLOX/AutoSaves
  12:24:22.281  OnClientEvent can only be used on the client  -  Server - ShowSurvivorScript:5
  12:24:22.282  Stack Begin  -  Studio
  12:24:22.282  Script 'ServerScriptService.ShowSurvivorScript', Line 5  -  Studio - ShowSurvivorScript:5
  12:24:22.282  Stack End  -  Studio
  12:24:22.282  ServerScriptService.GameLoop:101: Expected ',' when parsing index range, got 'in'  -  Studio - GameLoop:101

Game Loop

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game.Players:GetPlayers()
local ShowSurvivorEvent = ReplicatedStorage:WaitForChild("Events"):WaitForChild("ShowSurvivorEvent")
local ServerStorage = game:GetService("ServerStorage")
local MapsFolder = ServerStorage:WaitForChild("Maps")
local GameStatisticsGui = game.StarterGui.GameStatisticsGui
local SurvivorList = GameStatisticsGui:WaitForChild("SurvivorsFrame")
local WinReward = 1
local CoinReward = 25
local Status = ReplicatedStorage:WaitForChild("Values"):WaitForChild("Status")
local GameLength = 120
-- Game Loop
while true do

	Status.Value = "Waiting For 2 Players..."
	repeat wait() until game.Players.NumPlayers >= 1
	Status.Value = "Intermission..."
	wait(20)

	local plrs = {}
	for i, player in pairs(game.Players:GetPlayers()) do
		if player then
			table.insert(plrs,player) -- Adding Players
		end
	end

	wait(2)

	local AvaliableMaps = MapsFolder:GetChildren()
	local ChosenMap = AvaliableMaps[math.random(1, #AvaliableMaps)]

	Status.Value = "Map Chosen!"

	local ClonedMap = ChosenMap:Clone()
	ClonedMap.Parent = workspace

	-- Teleport Map
	local SpawnPoints = ClonedMap:FindFirstChild("SpawnPoints")
	if not SpawnPoints then
		warn("SpawnPoints Can't Be Found! Check if The Grouped Map Has Them!")
	end

	local AvaliableSpawnPoints = SpawnPoints:GetChildren()

	for i, player in pairs(plrs) do
		if player then
			character = player.Character

			if character then
				-- Teleport Them

				character:FindFirstChild("HumanoidRootPart").CFrame = AvaliableSpawnPoints[1].CFrame
				table.remove(AvaliableSpawnPoints,1)

				-- Game Tag
				local GameTag = Instance.new("BoolValue")
				GameTag.Name = "GameTag"
				GameTag.Parent = player.Character
			else
				warn("Player Can't Be Found!")
				if not player then
					table.remove(plrs,i)
				end
			end
		end
	end

	wait(2)
	Status.Value = "The Game Is Starting!"

	for i = GameLength,0,-1 do
		for x, player in pairs(plrs) do
			if player then
				character = player.Character
				if not character then
					-- Player Left
				else
					if character:FindFirstChild("GameTag") then
						-- Their Alive
					else
						-- Their Dead
						table.remove(plrs,x)
					end
				end
			else
				table.remove(plrs,x)
			end
		end

		Status.Value = "Time: "..i
		wait(1)

		if GameLength == 0 then
			for i, player in pairs(#plrs) do
				if player then
					for i, player in pairs(plrs) do
						if player then
							for i, GameStatisticsGui in pairs(plrs) do
								local GameStatisticsTextLabels = GameStatisticsGui.SurvivorsFrame:GetChildren()

								for i = 1 + plrs in pairs(#plrs) do
									GameStatisticsTextLabels[i] = plrs.Name
									if #plrs == 0 then
										Status.Value = "Nobody Won!"
									end
								end
							end
						end
					end
					character = player.Character

					if character then
					end
				end
			end
		end

		Status.Value = "End of game!"

		for i, player in pairs(game.Players:GetPlayers()) do
			character = player.Character
			if not character then
				-- Ignore
			else
				if character:FindFirstChild("GameTag") then
					character.GameTag:Destroy()
				end
			end
			player:LoadCharacter()
		end
	end
	ClonedMap:Destroy()
end

Show Survivors Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ShowSurvivorEvent = ReplicatedStorage:WaitForChild("Events"):WaitForChild("ShowSurvivorEvent")
local StarterGui = game:GetService("StarterGui")

ShowSurvivorEvent.OnClientEvent:Connect(function()
	StarterGui:WaitForChild("GameStatisticsGui").SurvivorsFrame.Visible = true
	StarterGui:WaitForChild("GameStatisticsGui").TitleLabel.Visible = true
	wait(10)
	StarterGui:WaitForChild("GameStatisticsGui").SurvivorsFrame.Visible = false
	StarterGui:WaitForChild("GameStatisticsGui").TitleLabel.Visible = false
end)