I Can't Unloop My Game Script

I was messing around with free-model scripts, for my new Squid Game-based game on Roblox, and I can’t unloop the game events.

  • Alright, so I have been working on a Red-Light, Green-Light Game on Roblox, where I have been trying to change up the scripts so I can advance in my knowledge, but I can’t manage to unloop the game events (Whenever I complete the events, it restarts and make me do everything). Does anyone know how to fix this loop.
local gameLength = 120


game.Players.PlayerAdded:Connect(function(plr)
	
	
	local ls = Instance.new("Folder", plr)
	ls.Name = "leaderstats"
	
	local cash = Instance.new("IntValue", ls)
	cash.Name = "Cash"
end)


local playersAlive = {}
local playersCompleted = {}

local redLight = false

game:GetService("RunService").Heartbeat:Connect(function()
	
	for i, player in pairs(game.Players:GetPlayers()) do

		if player.Character and player.Character:FindFirstChild("Humanoid") then

			if player.Character.Humanoid.MoveDirection.Magnitude > 0 and redLight and table.find(playersAlive, player) then
				
				player.Character.Humanoid.Health = 0
				
				script.Shot:Play()
			end
		end
	end
end)


while true do
	
	wait(1)
	
	
	workspace.SquidGame.PreGameBarrier.CanCollide = true
	workspace.SquidGame.EndGameBarrier.CanCollide = false
	
	workspace.SquidGame.Doll.Head.Orientation = Vector3.new(0, 0, 0)
	
	
	
	
	for i, player in pairs(game.Players:GetPlayers()) do
		
		player:LoadCharacter()
			
		table.insert(playersAlive, player)
		
		
		player.Character.Humanoid.Died:Connect(function()
			
			table.remove(playersAlive, table.find(playersAlive, player))
		end)
	end
	
	
	game.ReplicatedStorage.SquidGameRE:FireAllClients("status", "Game starting..", Color3.fromRGB(255, 255, 255))
	
	
	wait(30)
	
	workspace.SquidGame.PreGameBarrier.CanCollide = false
	
	
	local timeLeft = gameLength
	
	
	spawn(function()
		
		while timeLeft > 0 and #playersAlive > 0 do
			
			timeLeft -= 1
			
			local mins = math.floor(timeLeft / 60)
			local secs = timeLeft % 60
			
			if string.len(mins) < 2 then mins = "0" .. mins end
			if string.len(secs) < 2 then secs = "0" .. secs end
			
			game.ReplicatedStorage.SquidGameRE:FireAllClients("timer", mins .. ":" .. secs)
			
			wait(1)
		end
	end)
	
	
	workspace.SquidGame.EndGameBarrier.Touched:Connect(function(hit)
		
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		
		if player then
			
			table.remove(playersAlive, table.find(playersAlive, player))
			
			table.insert(playersCompleted, player)
			
			game.ReplicatedStorage.SquidGameRE:FireClient(player, "barrier", workspace.SquidGame.EndGameBarrier)
		end
	end)
	
	
	while timeLeft > 0 and #playersAlive > 0 do
		
		
		game.ReplicatedStorage.SquidGameRE:FireAllClients("status", "GREEN LIGHT", Color3.fromRGB(0, 255, 0))
		
		
		local randomSpeed = math.random(10, 13)/10
		script.Sound.PlaybackSpeed = randomSpeed
		
		
		script.Sound:Play()
		
		script.Sound.Ended:Wait()
		
		
		game:GetService("TweenService"):Create(workspace.SquidGame.Doll.Head, TweenInfo.new(0.3), {Orientation = Vector3.new(0, 180, 0)}):Play()
		redLight = true
		
		game.ReplicatedStorage.SquidGameRE:FireAllClients("status", "RED LIGHT", Color3.fromRGB(255, 0, 0))
		
		wait(math.random(1, 5))
		
		game:GetService("TweenService"):Create(workspace.SquidGame.Doll.Head, TweenInfo.new(0.3), {Orientation = Vector3.new(0, 0, 0)}):Play()
		redLight = false
	end
	
	
	for i, player in pairs(playersAlive) do
		
		player.Health = 0
		
		script.Shot:Play()
	end
	
	for i, player in pairs(playersCompleted) do
		
		player.leaderstats.Cash.Value += math.floor(0 / #playersCompleted)
	end
end
  • I tried changing the following script to false:
while true do
	
	wait(1)
  • In result of this, on joining of the game, the camera view would be stuck in a random position of the map. I don’t know whether this is due to any other game settings or just the Game Event Script.

  • If you know a solution, please state so.

What do you mean by unloop? Please give us an example and a definition.

1 Like

Well, whenever I manage to get to the end of the game, it restarts the events and I have to redo it.

What do you mean by restarts the event?

1 Like

So, when you join, you have to do a game called Red-Light, Green-Light. On completion of the time or everyone making to the end, it restarts and you do another round, but I don’t want that to happen and I just want it to end when people complete it in the time-period. Sorry if I wasn’t precise.

Setting that to false before the game has completed should fix the issue but you will still have to wait for the round to end.

1 Like

I assume that you need to check if the timeLeft variable is <= 0, just so this indicates when the times out, you end the game.

If all the players had pass through the finish line, end the game as well.

To end the game, just use the break block to break a loop.

1 Like

When I did that, I wasn’t able to join the game properly, as my camera was stuck in a random position of the map and it wouldn’t load in my actual character.

That is because you never loaded your character when it is false, this script needs you to call :LoadCharacter() as that is the way you have set it up.

1 Like

Could you place this within the script for me as I don’t really have too much understanding within Lua yet. Thanks!