Script loops between lines 17 and 22 & is not showing an error

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to make a round system.

  2. What is the issue? Include screenshots / videos if possible!
    I made a script which is very bad for my feeling. It’s not showing up an error code but the script isn’t working. It’s continuously repeating the Print from lines 17 and 22.
    But I think it’s no the while loop doing that because there is more text inside. And by the last end is an error line underneath it.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I looked for a solution but couldn’t find them.

NOTE: I am not an advanced scripter, I know that this script is terrible.

local catText = game.StarterGui.ScreenGui.Cat.catText
local mapSave = game:GetService("ReplicatedStorage"):WaitForChild("Maps")
local gamePlayers = game:GetService("Players")

-- Values
local roundTime = 60
local intermissionTime = 15
local loadTime = 10
local mapsCount = 3

local intermission = ("Intermission")
local gameBegin = ("Round Begins")
local roundProgress = ("Round in progress")
local roundProgressBoolian = game:GetService("ServerStorage"):FindFirstChild("roundVal")

while true do function map()
	print("Works")
	-- Intermission starts
	wait(intermissionTime)
	
	-- Loads the map in
	print("Works")
	local maps = mapSave:GetChildren()
	local mapChoose = maps[math.random(1,3)]
	
	if mapChoose == 1 then
		local findChild = mapSave:WaitForChild("CandyMap")
		local mapSpawn = findChild:Clone()
		mapSpawn.Parent = workspace
		local succes, value = pcall(function()
			return mapChoose
		end)
		
		if (succes) then 
			print("Map loaded_"..("Map:")..(value))
		else
			warn("Uh- oh something went wrong. Error code 102!"..("We tried to load in map:")(value))
		end
		print("Works")
	elseif mapChoose == 2 then
		local findChild = mapSave:WaitForChild("StandardMap")
		local mapSpawn = findChild:Clone()
		mapSpawn.Parent = workspace
		local succes, value = pcall(function()
			return mapChoose
		end)

		if (succes) then 
			print("Map loaded_"..("Map:")..(value))
		else
			warn("Uh- oh something went wrong. Error code 102!"..("We tried to load in map:")(value))
		end
		print("Works")
	elseif mapChoose == 3 then
		local findChild = mapSave:WaitForChild("SnowMap")
		local mapSpawn = findChild:Clone()
		mapSpawn.Parent = workspace
		local succes, value = pcall(function()
			return mapChoose
		end)

		if (succes) then 
			print("Map loaded._"..("Map:")..(value))
		else
			warn("Uh- oh something went wrong. Error code 102!"..("We tried to load in map:")(value))
	end
		print("Works")
	-- Teleport's players
	wait(loadTime)
	spawn(function()
		for _,plr in pairs(gamePlayers:GetPlayers()) do
			local spawnChoose = mapSpawn
			if plr and plr.Parent and plr.Character and plr.Character.PrimaryPart then
				plr.Character.PrimaryPart:SetPrimaryPartCFrame(spawnChoose.CFrame)
				roundProgressBoolian.Value = true
			end
		end
	end)
		print("Works")
	-- Round progress
	if roundProgressBoolian == true then 
		local repStor = game:GetService("ReplicatedStorage")
		local timEvent = repStor:FindFirstChild("TimerEvent")
		timEvent:FireServer()
		wait(roundTime)
		roundProgressBoolian.Value = false
		end
end)


Please can someone the fault?

EDIT: This is what the output says when i start the game:

09:18:11.378  Samurai Elements.rbxl auto-recovery file was created
  09:18:11.892  Works  -  Server - Round:17
  09:18:26.907  Works  -  Server - Round:22
  09:18:26.908  Works  -  Server - Round:17
  09:18:41.923  Works  -  Server - Round:22
  09:18:41.923  Works  -  Server - Round:17
  09:18:56.940  Works  -  Server - Round:22
  09:18:56.940  Works  -  Server - Round:17
  09:18:59.847  Disconnect from ::ffff:  -  Studio
  09:22:24.291   ▶ Samurai Elements.rbxl auto-recovery file was created (x2)

The problem is this line. You are taking a random child out of the folder, but then you are checking if that variable is equal to a number, so it will never be true.

Just change it to:

local mapChoose = math.random(1,3)
1 Like

Can u help me fast with something else? Now i do the map spawn with an If statement but it glitches out. Can i also use an iv pairs loop?

I don’t think I understand what you mean, can you explain more please.

So i provided in my script these lines:

if mapChoose == 1 then
		local findChild = mapSave:WaitForChild("CandyMap")
		local mapSpawn = findChild:Clone()
		mapSpawn.Parent = workspace
		local succes, value = pcall(function()
			return mapChoose
		end)
		
		if (succes) then 
			print("Map loaded_"..("Map:")..(value))
		else
			warn("Uh- oh something went wrong. Error code 102!"..("We tried to load in map:")(value))
		end
		print("Works")
	elseif mapChoose == 2 then
		local findChild = mapSave:WaitForChild("StandardMap")
		local mapSpawn = findChild:Clone()
		mapSpawn.Parent = workspace
		local succes, value = pcall(function()
			return mapChoose
		end)

		if (succes) then 
			print("Map loaded_"..("Map:")..(value))
		else
			warn("Uh- oh something went wrong. Error code 102!"..("We tried to load in map:")(value))
		end
		print("Works")
	elseif mapChoose == 3 then
		local findChild = mapSave:WaitForChild("SnowMap")
		local mapSpawn = findChild:Clone()
		mapSpawn.Parent = workspace
		local succes, value = pcall(function()
			return mapChoose
		end)

		if (succes) then 
			print("Map loaded._"..("Map:")..(value))
		else
			warn("Uh- oh something went wrong. Error code 102!"..("We tried to load in map:")(value))
	end

But now it loops through the math.random until it’s equal to 1, 2 or 3
now I want that the script is checking the result of math.random to look at which map the script needs to choose and clone.

Try this:

local catText = game.StarterGui.ScreenGui.Cat.catText
local mapSave = game:GetService("ReplicatedStorage"):WaitForChild("Maps")
local gamePlayers = game:GetService("Players")

-- Values
local roundTime = 60
local intermissionTime = 15
local loadTime = 10
local mapsCount = 3

local intermission = ("Intermission")
local gameBegin = ("Round Begins")
local roundProgress = ("Round in progress")
local roundProgressBoolian = game:GetService("ServerStorage"):FindFirstChild("roundVal")

while true do function map()
	print("Works")
	-- Intermission starts
	wait(intermissionTime)
	
	-- Loads the map in
	print("Works")
	local maps = mapSave:GetChildren()
	local ChosenMap = maps[math.random(1,#maps)]
	local mapSpawn = ChosenMap:Clone()
        mapSpawn.Parent = workspace

	-- Teleport's players
	wait(loadTime)
	spawn(function()
		for _,plr in pairs(gamePlayers:GetPlayers()) do
			local spawnChoose = mapSpawn
			if plr and plr.Parent and plr.Character and plr.Character.PrimaryPart then
				plr.Character.PrimaryPart:SetPrimaryPartCFrame(spawnChoose.CFrame)
				roundProgressBoolian.Value = true
			end
		end
	end)
		print("Works")
	-- Round progress
	if roundProgressBoolian == true then 
		local repStor = game:GetService("ReplicatedStorage")
		local timEvent = repStor:FindFirstChild("TimerEvent")
		timEvent:FireServer()
		wait(roundTime)
		roundProgressBoolian.Value = false
		end
end)

It says:
ServerScriptService.RoundTest:25: attempt to index number with 'Clone' - Server - RoundTest:25

The mapSave variable is a folder with all the maps in it?

It is working already, thanks for your help!

1 Like