My Game Handler Won't Work =/

Hello! so I’ve been trying to make a Infinite Warfare type game, and I got a script but It wont work, I made it so when the timer of the round goes out then the map Destroy()'s itself and then 20 sec intermission on a Baseplate, The Issue is it doesnt even chose a map at the start and I got this error,

  12:34:31.924  ServerScriptService.GameHandler:99: Expected 'then' when parsing if statement, got 'table'  -  Studio - GameHandler:99
  12:34:45.943   ▶ Unable to load plugin icon. (x2)  -  Studio --Ignore>>

<<< The Script >>>

-- Define Variables

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local ServerStorage = game:GetService("ServerStorage")

local MapsFolder = ServerStorage:WaitForChild("Maps")

local Status = ReplicatedStorage:WaitForChild("Status")

local GameLength = 300

-- Game Loop

while true do


	Status.Value = "Waiting for enough players..."

	repeat wait() until game.Players.NumPlayers >= 2

	Status.Value = "Intermission..."

	wait(20)

	local plrs = {}

	for i, player in pairs (game.Players:GetPlayers()) do
		if player then
			table.insert(plrs,player) -- Add each player into plrs table
		end
	end

	wait(2)

	local AvalibleMaps = MapsFolder:GetChildren()

	local ChosenMap = AvalibleMaps[math.random(1,#AvalibleMaps)]

	Status.Value = ChosenMap.Name.."Chosen"

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

	-- Telaports players to the map

	local SpawnPoints = ClonedMap:FindFirstChild("SpawnPoints")

	if not SpawnPoints then
		print(" { ERROR 101 } SpawnPoints not found!")
	end

	local AvalibleSpawnPoints = SpawnPoints:GetChildren()

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

			if character then
				-- Telaport them

				character:FindFirstChild("HumanoidRootPart").CFrame = AvalibleSpawnPoints[1].CFrame
				table.remove(AvalibleSpawnPoints,1)
				
				local GameTag = Instance.new("BoolValue")
				GameTag.Name = "GameTag"
				GameTag.Parent = player.Character
			else
				-- Their is no character
				if not player then
					table.remove(plrs, i)
				end
			end
		end
	end


	Status.Value = "Get Ready To Play!"

	wait(2)

	for i = GameLength,0,-1 do

		for x, player in pairs(plrs) do
			if player then

				character = player.Character

				if not character then
					-- Left the game
				else
					if character:FindFirstChild("GameTag") then
						-- They are still alive
						wait(1)
						print(player.Name.." is still in the game!")
					else
						-- Game Time = Over
						if GameLength == 0
						table.remove(plrs,x)
						print(player.Name.."Has been removed!")
					end
				end
			else
				table.remove(plrs,x)
				print(player.Name.."Has been removed!")

	print("End of game")

	wait(2)

	for i, player in pairs(game.Players:GetPlayers()) do
		character = player.Character

		if not character then
			--Ignore them
		else
			if character:FindFirstChild("GameTag") then
				character.GameTag:Destroy()
			end

			if player.Backpack:FindFirstChild("Sword") then
				player.Backpack.Sword:Destroy()
			end
			if character:FindFirstChild("Sword") then
				character.Sword:Destroy()
			end
		end

	end

	ClonedMap:Destroy()

	Status.Value = "The Game has ended!"
end

I don’t know how to fix it, please help.

1 Like

You had a few missing ends, particularly right here

That bit should be this

for i = GameLength,0,-1 do

	for x, player in pairs(plrs) do
		if player then

			character = player.Character

			if character then
				if character:FindFirstChild("GameTag") then
					-- They are still alive
					wait(1)
					print(player.Name.." is still in the game!")
				elseif GameLength == 0 then
					table.remove(plrs,x)
					print(player.Name.."Has been removed!")
				end
			end
		else
			table.remove(plrs,x)
			print(player.Name.."Has been removed!")
		end
	end
end
1 Like
13:26:50.762  ServerScriptService.GameHandler:101: Expected 'then' when parsing if statement, got 'table'  -  Studio - GameHandler:101

May I see the line that is erroring?

1 Like
101	       table.remove(plrs,x)

Do you see any red lines in the script editor? If so, can you screenshot them

Line 68 & 73 have red underlines for the "x’ (plrs,x)

( my screenshotting thing isnt working well so I have to type it )

Thing that’s a fault of my own in that case since I didn’t see you were using the index, change _ to x, and as for your error, it has to be the thing I mentioned because the only if statement without a then is

if GameLength == 0

Which I fixed in my code, are you sure you changed it properly? May I see your code after doing my suggested solution with the ends

1 Like

whats an index

and also what line is the _ that im supposed to change to x number?

-- Define Variables

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local ServerStorage = game:GetService("ServerStorage")

local MapsFolder = ServerStorage:WaitForChild("Maps")

local Status = ReplicatedStorage:WaitForChild("Status")

local GameLength = 300

-- Game Loop

while true do


	Status.Value = "Waiting for enough players..."

	repeat wait() until game.Players.NumPlayers >= 2

	Status.Value = "Intermission..."

	wait(20)

	local plrs = {}

	for i, player in pairs (game.Players:GetPlayers()) do
		if player then
			table.insert(plrs,player) -- Add each player into plrs table
		end
	end

	wait(2)

	local AvalibleMaps = MapsFolder:GetChildren()

	local ChosenMap = AvalibleMaps[math.random(1,#AvalibleMaps)]

	Status.Value = ChosenMap.Name.."Chosen"

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

	-- Telaports players to the map

	local SpawnPoints = ClonedMap:FindFirstChild("SpawnPoints")

	if not SpawnPoints then
		print(" { ERROR 101 } SpawnPoints not found!")
	end

	local AvalibleSpawnPoints = SpawnPoints:GetChildren()

	for i = GameLength,0,-1 do

		for _, player in pairs(plrs) do
			if player then

				character = player.Character

				if character then
					if character:FindFirstChild("GameTag") then
						-- They are still alive
						wait(1)
						print(player.Name.." is still in the game!")
					elseif GameLength == 0 then
						table.remove(plrs,x)
						print(player.Name.."Has been removed!")
					end
				end
			else
				table.remove(plrs,x)
				print(player.Name.."Has been removed!")
			end
		end
	end


	Status.Value = "Get Ready To Play!"

	wait(2)

	for i = GameLength,0,-1 do

		for x, player in pairs(plrs) do
			if player then

				character = player.Character

				if not character then
					-- Left the game
				else
					if character:FindFirstChild("GameTag") then
						-- They are still alive
						wait(1)
						print(player.Name.." is still in the game!")
					else
						-- Game Time = Over
						if GameLength == 0
						table.remove(plrs,x)
						print(player.Name.."Has been removed!")
					end
				end
			else
				table.remove(plrs,x)
				print(player.Name.."Has been removed!")

	print("End of game")

	wait(2)

	for i, player in pairs(game.Players:GetPlayers()) do
		character = player.Character

		if not character then
			--Ignore them
		else
			if character:FindFirstChild("GameTag") then
				character.GameTag:Destroy()
			end

			if player.Backpack:FindFirstChild("Sword") then
				player.Backpack.Sword:Destroy()
			end
			if character:FindFirstChild("Sword") then
				character.Sword:Destroy()
			end
		end

	end

	ClonedMap:Destroy()

	Status.Value = "The Game has ended!"
end

You copied incorrectly, try this

-- Define Variables

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local ServerStorage = game:GetService("ServerStorage")

local MapsFolder = ServerStorage:WaitForChild("Maps")

local Status = ReplicatedStorage:WaitForChild("Status")

local GameLength = 300

-- Game Loop

while true do


	Status.Value = "Waiting for enough players..."

	repeat wait() until game.Players.NumPlayers >= 2

	Status.Value = "Intermission..."

	wait(20)

	local plrs = {}

	for i, player in pairs (game.Players:GetPlayers()) do
		if player then
			table.insert(plrs,player) -- Add each player into plrs table
		end
	end

	wait(2)

	local AvalibleMaps = MapsFolder:GetChildren()

	local ChosenMap = AvalibleMaps[math.random(1,#AvalibleMaps)]

	Status.Value = ChosenMap.Name.."Chosen"

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

	-- Telaports players to the map

	local SpawnPoints = ClonedMap:FindFirstChild("SpawnPoints")

	if not SpawnPoints then
		print(" { ERROR 101 } SpawnPoints not found!")
	end

	local AvalibleSpawnPoints = SpawnPoints:GetChildren()
	
	for i, player in pairs(plrs) do
		if player then
			character= player.character

			if character then
				-- Telaport them

				character:FindFirstChild("HumanoidRootPart").CFrame = AvalibleSpawnPoints[1].CFrame
				table.remove(AvalibleSpawnPoints,1)
				
				local GameTag = Instance.new("BoolValue")
				GameTag.Name = "GameTag"
				GameTag.Parent = player.Character
			else
				-- Their is no character
				if not player then
					table.remove(plrs, i)
				end
			end
		end
	end

	Status.Value = "Get Ready To Play!"

	wait(2)

	for i = GameLength,0,-1 do

		for x, player in pairs(plrs) do
			if player then

				character = player.Character

				if character then
					if character:FindFirstChild("GameTag") then
						-- They are still alive
						wait(1)
						print(player.Name.." is still in the game!")
					elseif GameLength == 0 then
						table.remove(plrs,x)
						print(player.Name.."Has been removed!")
					end
				end
			else
				table.remove(plrs,x)
				print(player.Name.."Has been removed!")
			end
		end
	end

	wait(2)

	for i, player in pairs(game.Players:GetPlayers()) do
		character = player.Character

		if not character then
			--Ignore them
		else
			if character:FindFirstChild("GameTag") then
				character.GameTag:Destroy()
			end

			if player.Backpack:FindFirstChild("Sword") then
				player.Backpack.Sword:Destroy()
			end
			if character:FindFirstChild("Sword") then
				character.Sword:Destroy()
			end
		end

	end

	ClonedMap:Destroy()

	Status.Value = "The Game has ended!"
end
1 Like

It worked, for some reason now theirs a infinite yield

  13:55:30.035  Infinite yield possible on 'ServerStorage:WaitForChild("Maps")'  -  Studio
  13:55:30.035  Stack Begin  -  Studio
  13:55:30.035  Script 'ServerScriptService.GameHandler', Line 7  -  Studio - GameHandler:7
  13:55:30.035  Stack End  -  Studio

Are you certain there’s a Maps folder in ServerStorage? Could it be taht the one in ServerStorage is named differently?

1 Like

nope not at all, its named “Maps” same thing so Idk how a infinite yield can happen because all of my maps are anchored.