Unknown Global while i have a variable

So i have this game where i have a bunch of different gamemodes, i added them one by one. but now when i want to add the 4th one it says unknown global ‘map’ while i created a variable and it worked throughout the whole script but just not when i tried to create the 4th gamemode. does anyone have an idea? script and error below.

local status = game.ReplicatedStorage.Status
local maps = game.ReplicatedStorage.Maps:GetChildren()
local AlivePlayers = {}

while true do
	status.Value = "Waiting For Players"
	repeat task.wait() status.Value = "Waiting for a player to join" until #game.Players:GetPlayers() >= 1
	if #game.Players:GetPlayers() >= 1 then
		status.Value = "Starting"
		for i = 1,15 do
			status.Value = "Next round will start in: "..15-i
			task.wait(1)
		end

		local rand = math.random(1, #maps)

		local map = maps[rand]:Clone()
		map.Parent = workspace

		status.Value = "The next minigame is: "..map.Name
		task.wait(5)


		local players = game.Players:GetPlayers()
		for i = 1,#players do
			if players[i].Character ~= nil then
				local spawnLocation = math.random(1,#map.Teleports:GetChildren())
				players[i].Character:MoveTo(map.Teleports:GetChildren()[spawnLocation].Position)
				players[i].Character.Parent = workspace.Ingame

			end
		end

		local racewinners = {}
		local survivalwinners = {}
		local roundLenght = 10
		local canWin = true
		local roundType = ""

		if map:FindFirstChild("Race") then
			roundType = "Race"
			map.WinnerPart.Touched:Connect(function(hit)
				if hit.Parent:FindFirstChild("Humanoid") and canWin == true then
					if #racewinners <= 3 then -- checks amount of values in the table
						local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
						if not table.find(racewinners,plr.Name) then
							table.insert(racewinners,plr.Name)
							status.Value = hit.Parent.Name.." Has won!"
							task.wait(1)
							plr.leaderstats.Wins.Value += 1
							plr.leaderstats.Cash.Value += 100
							plr:LoadCharacter()
						end
					else
						canWin = false
					end					
				end
			end)

			if roundType == "Race" then
				repeat
					roundLenght = roundLenght -1
					status.Value = "Time left: "..roundLenght.."\n Current Winners: "..table.concat(racewinners,",")
					task.wait(1)
				until roundLenght == 0 or canWin == false or #workspace.Ingame:GetChildren() == 0
				status.Value = "Winner(s): "..table.concat(racewinners,",")
				task.wait(3)
				table.clear(racewinners)

				task.wait(3)
				map:Destroy()
			end

		elseif map:FindFirstChild("Survival") then
			roundType = "Survival"
			AlivePlayers = game:GetService("Players"):GetChildren()

			local connection
			for _, player in pairs(game:GetService("Players"):GetChildren()) do
				local char = player.Character
				connection = char:WaitForChild("Humanoid").Died:Connect(function() -- runs the function when player has died
					print(player.Name .. " has died!")
					if table.find(AlivePlayers, game:GetService("Players"):FindFirstChild(player.Name)) then -- wont run if the player isn't in the table
						connection:Disconnect() -- This part makes it so if the same player dies again the script wont run. This also reduces lag so yes. If you want to make it so the script keeps running even though the player has died once then remove this part
						local location = table.find(AlivePlayers, game:GetService("Players"):FindFirstChild(player.Name)) -- gets the location of player in table
						table.remove(AlivePlayers, location) -- removes the player from tabl
					end
				end)
			end

			if roundType == "Survival" then
				repeat
					roundLenght = roundLenght -1
					status.Value = "Time left: "..roundLenght
					task.wait(1)
				until roundLenght == 0 or #workspace.Ingame:GetChildren() == 0
				for _, plr in pairs(AlivePlayers) do
					plr.leaderstats.Wins.Value += 1
					plr.leaderstats.Cash.Value += 75
				end
				status.Value = "Winner(s): "
				for i,v in pairs(AlivePlayers) do
					status.Value = status.Value.. ", " ..AlivePlayers[i].Name
				end

				task.wait(3)
				table.clear(AlivePlayers)
				map:Destroy()
			end 
			
		elseif map:FindFirstChild("LastOneStandingWeapon") then
			roundType = "LastOneStandingWeapon"
			
			local children = workspace.Ingame:GetChildren()
			for i = 1,#children do
				map:FindFirstChildWhichIsA("Tool"):Clone().Parent = children[i]
			end
			
			map:FindFirstChildWhichIsA("Tool"):Destroy()	
		end
		if roundType == "LastOneStandingWeapon" then
			repeat roundLenght = roundLenght -1
				status.Value = "Time left: "..roundLenght
				task.wait(1)
			until roundLenght == 0 or #workspace.Ingame:GetChildren() == 1
			
			if #workspace.Ingame:GetChildren() == 1 and roundType == "LastOneStandingWeapon" then
				local char = workspace.Ingame:FindFirstChildWhichIsA("Model")
				
				status.Value = "Winner: "..char.Name
				
				local plr = game.Players:GetPlayerFromCharacter(char)
				plr.leaderstats.Wins.Value += 1
				plr.leaderstats.Cash.Value += 125				
			end
			
			task.wait(3)
			map:Destroy()		
		end
		
	elseif map:FindFirstChild("LastOneStanding") then
		roundType = "LastOneStanding"
					
		
		local players = game.Players:GetPlayers()
		for i = 1,#players do
			if players[i].Character ~= nil then
				players[i]:LoadCharacter()
			end
		end
	end
end

image
‘Unknown global map’

2 Likes

The problem is the position of your closing end statements. The map variable is declared within the if #game.Players:GetPlayers() >= 1 then statement, but elseif map:FindFirstChild("LastOneStanding") then is outside due to the ends.

Comment out the previous end and a new one after roundType = "LastOneStanding", ie:

		if roundType == "LastOneStandingWeapon" then
			repeat roundLenght = roundLenght -1
				status.Value = "Time left: "..roundLenght
				task.wait(1)
			until roundLenght == 0 or #workspace.Ingame:GetChildren() == 1

			if #workspace.Ingame:GetChildren() == 1 and roundType == "LastOneStandingWeapon" then
				local char = workspace.Ingame:FindFirstChildWhichIsA("Model")

				status.Value = "Winner: "..char.Name

				local plr = game.Players:GetPlayerFromCharacter(char)
				plr.leaderstats.Wins.Value += 1
				plr.leaderstats.Cash.Value += 125				
			end

			task.wait(3)
			map:Destroy()		
	--	end

		elseif map:FindFirstChild("LastOneStanding") then
			roundType = "LastOneStanding"
		end	

			local players = game.Players:GetPlayers()
	
			for i = 1,#players do
			if players[i].Character ~= nil then
				players[i]:LoadCharacter()
			end
			end
			
	end
end
1 Like

The problem is you’re using a local variable outside the statement where the local variable is located. Make a global variable by removing “local”

http://lua-users.org/wiki/ScopeTutorial

So this was indeed the solution, but could you explain me why it works this way?. Because in the other blocks of code theres and end after map:Destroy() so why shouldnt there be one after this block? I would appreciate if u can explain me this so i can learn from it :smiley:

These articles, as explained by Roblox are better than me trying to explain:

There are loads more useful things hidden in the Roblox Education site:
https://developer.roblox.com/en-us/learn-roblox/all-tutorials#articles