Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I restart a loop or script in the middle of the process?

I am trying to make a game with survivors and a monster. I have set where it I choose a random player as a monster and everyone else as a survivor. Once it has chosen a monster, the survivors and the monster are teleported into the map. Then a timer will appear and will start counting down. When the timer reaches zero seconds, everyone on the map will be teleported back to the lobby and the game starts over again.It's on a while loop so the game never ends. The problem is if someone is the monster and they leave, how can I make the players on the map spawn back to the lobby and restart the loop so the timer will not continue while everyone is in the lobby.

Edit:

while wait() do
	-- Assign roles
	survivors = = game:GetService("Players"):GetPlayers()
	monster = players[math.random(1, #players)]
	timer = 50

	-- Teleport players to map
	for _, player in pairs(game:GetService("Players"):GetPlayers()) do
		player.Character.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(x ,y ,z))
	end
	
	-- Starts the timer
	repeat
		timer = timer - 1
		wait(1)
	until timer == -1
	
	print("Game ended!")
	
	--Teleport back to lobby
	for _, player in pairs(game:GetService("Players"):GetPlayers()) do
		player.Character.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(x ,y ,z))
	end
end

3 comments

0

It'd be nice if you'd show us the script.

0

put while true do at the top of the script you want to repeat and end at the bottom of the script you want to reapeat

0

I updated it with the code. I don't think I needed to add it but oh well

1 answer

✓ Accepted answer

This, although it can be a very easy fix, can become a very annoying one to fix depending on how much you have in your while loop.

Proposed solution #1: Instead of using a while loop, use an event that runs every 1/40th of a second, for instance, RunService.Stepped and check to make sure that the player is still in the game.

Example of this in a loop game:

local RunService = game:GetService("RunService")
local ChosenPlayer = game.Players[math.random(1,#game.Players)]

RunService.Stepped:connect(function()
	local ChosenPlayerInGame = false
	pcall(function()
		if game.Players:findFirstChild(ChosenPlayer.Name) then
			ChosenPlayerInGame = true
		end
	end)

	if ChosenPlayerInGame then
		-- Insert stuff to happen every 1/40th of a second
	end
end)

FURTHERMORE, using this solution means that you would have to create a timer using the tick() function. The timer is needed in order to time the whole game, since this is possible through loops but not possible through

Example of timer:

local RunService = game:GetService("RunService")
local ChosenPlayer = game.Players[math.random(1,#game.Players)]
local Timer = 60 -- In seconds
local GameRunning = false

local CurTime = Timer
local LastTick = tick()
RunService.Stepped:connect(function()
	local ChosenPlayerInGame = false
	pcall(function()
		if game.Players:findFirstChild(ChosenPlayer.Name) then
			ChosenPlayerInGame = true
		end
	end)

	if ChosenPlayerInGame then
		-- Insert stuff to happen every 1/40th of a second
	end

	if GameRunning then
		CurTime = CurTime - (tick()-LastTick)
		if CurTime <= 0 then
			-- Insert stuff to happen once game ends
		end
	end

	LastTick = tick()
end)

Proposed soltuion #2: Instead of using the wait() function to wait for the round to end, you can use a repeat until loop to wait instead. This allows you to repeat the wait() function until either the timer ends, all the players leave, OR until the chosen player leaves.

Example:

while true do
	-- Stuff that happens first, whatever that may be in your case

	local Timer = 40
	repeat 
		Timer = Timer - wait() 
until Timer <= 0 or not game.Players:findFirstChild(ChosenPlayer.Name) or #SurvivingPlayers == 0
	
	if Timer <= 0 then
		-- What happens if the timer runs out and survivors win
	elseif not game.Players:findFirstChild(ChosenPlayer.Name) then
		-- What happens if the chosen monster leaves the game
	elseif #SurvivingPlayers == 0 then
		-- What happens in the surviving players all lose or leave
	end
end

Basically, the repeat function allows something to keep repeating until one of the until conditions in met. This allows for you to also code in things that happen if the condition was met. (I used ChosenPlayer to account for the chosen monster, and SurvivngPlayers to account for the players that are alive)

I hope this made sense! :)

EDIT: I just noticed you posted the script on the question, I am so sorry I would have made this answer much less confusing :(

3 comments

0

Lots of useful information! Thank you! I didn't know about some of these so I will try to learn and implement them into my game

0

This is not confusing. This is wonderful information for me. I appreciate it!

0

I'm here to help! Glad it did! :D

This discussion is preserved as it was. New answers, comments, and votes are closed.