Why isn't my script working?

So pretty much this script puts things in and out of the workspace but it’s not working for some reason

local lobbyLocation = game.Workspace.Lobby.Position + Vector3.new(0, 3, 0)
local gameLocation = game.Workspace.Main.Position + Vector3.new(0, 3, 0)

local Mexico = game.Workspace.Mexico
local Russia = game.Workspace.Russia
local USA = game.Workspace.USA
local Japan = game.Workspace.Japan
local Australia = game.Workspace.Australia

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local timeEvent = ReplicatedStorage:WaitForChild("TimeEvent")

local location = math.random(1, 5)

local function playGame()
	
	if Mexico == nil and game.Workspace.Country_Randomizer.Value.Value ~= 3 then
		game.Workspace.Mexico.Parent = game.ServerStorage
	end
	
	if Russia == nil and game.Workspace.Country_Randomizer.Value.Value ~= 1 then
		game.Workspace.Russia.Parent = game.ServerStorage
	end
	
	if USA == nil and game.Workspace.Country_Randomizer.Value.Value ~= 2 then
		game.Workspace.USA.Parent = game.ServerStorage
	end
	
	if Japan == nil and game.Workspace.Country_Randomizer.Value.Value ~= 4 then
		game.Workspace.Japan.Parent = game.ServerStorage
	end
	
	if Australia == nil and game.Workspace.Country_Randomizer.Value.Value ~= 5 then
		game.Workspace.Australia.Parent = game.ServerStorage
	end
	
	local timeAmount = 10
	local timerText = "Time until we leave: "
	while timeAmount > 0 do
		timeEvent:FireAllClients(timeAmount, timerText)
		wait(1)
		timeAmount -= 1
	end
end

local function playIntermission()
	
	location = math.random(1, 5)
	
	if location == 1 then 
		game.Workspace.Country_Randomizer.SurfaceGui.TextLabel.Text = "Russia"
		game.Workspace.Country_Randomizer.Value.Value = 1
		game.Workspace.Russia.Parent = game.Workspace
	end
	
	if location == 2 then 
		game.Workspace.Country_Randomizer.SurfaceGui.TextLabel.Text = "USA"
		game.Workspace.Country_Randomizer.Value.Value = 2
		game.Workspace.USA.Parent = game.workspace
	end
	
	if location == 3 then 
		game.Workspace.Country_Randomizer.SurfaceGui.TextLabel.Text = "Mexico"
		game.Workspace.Country_Randomizer.Value.Value = 3
		game.Workspace.Mexico.Parent = game.Workspace
	end
	
	if location == 4 then 
		game.Workspace.Country_Randomizer.SurfaceGui.TextLabel.Text = "Japan"
		game.Workspace.Country_Randomizer.Value.Value = 4
		game.Workspace.Japan.Parent = game.Workspace
	end
	
	if location == 5 then 
		game.Workspace.Country_Randomizer.SurfaceGui.TextLabel.Text = "Australia"
		game.Workspace.Country_Randomizer.Value.Value = 5
		game.Workspace.Australia.Parent = game.Workspace
	end
	
	local intermission = 20
	local timerText = "Time until we travel: "
	while intermission > 0 do
		timeEvent:FireAllClients(intermission, timerText)
		wait(1)
		intermission -= 1
	end
end

local function resetPlayers()

	for _, plr in pairs(game.Players:GetChildren()) do
		plr.Character.HumanoidRootPart.CFrame = CFrame.new(lobbyLocation)
	end
end

local function teleportPlayers()
	for _, plr in pairs(game.Players:GetChildren()) do
		plr.Character.HumanoidRootPart.CFrame = CFrame.new(gameLocation)
	end
end	

while true do
	resetPlayers()
	playIntermission()
	teleportPlayers()
	playGame()
end

That’s the script and it’s not working…

Do you get any errors in the output

Where do you actually fire the functions?

Try wrapping your while true do loop into a spawn(function()
The problem may be that your loop is suspending the rest of the code from working.
Unless you have a spawn(function() or a coroutine.new(), you can not have a single script working on 2 different lines.

Quick explanation if you care:
If you would put this in a script:

while wait() do
   print("first loop")
end

while wait() do
   print("second loop")
end

It would only keep printing “first loop”, since it resets the script to your first loop and never reaches the second one

Do NOT use a spawn(function() please…

Use Coroutine.new it is far more efficient.

Spawn loops have a delay before they run, which can increase exponentially.

1 Like