How to repeat a script after something happens?

So I’m currently making a game and I need the script to reset once the chosen player dies. I tried using a return but to no avail. Any help is appreciated!

local players = game.Players

local Intermission = 10

local NormalMaps = game:GetService("ReplicatedStorage"):WaitForChild("Maps").NormalMaps:GetChildren()

local Announcement = script.Announcement

local gameisactive = false

wait(2)
while true do
	if gameisactive == false then
		Intermission = 10
		Announcement.Value = "Picking Giant."
		wait(.5)
		Announcement.Value = "Picking Giant.."
		wait(.5)
		Announcement.Value = "Picking Giant..."
		local Giant = players:GetPlayers()[math.random(1, #players:GetPlayers())]
		Announcement.Value = Giant.Name.. " has been Chosen as the Giant!"
		workspace.ActiveMap:ClearAllChildren()
		local ChosenMap = NormalMaps[math.random(1, #NormalMaps)]
		ChosenMap.Parent = workspace.ActiveMap
		repeat wait(1)
			Intermission -=1
			Announcement.Value = Intermission
		until Intermission == 0
		Announcement.Value = "Game Started!"
		gameisactive = true
		while wait() do
		if Giant.Character:WaitForChild("Humanoid").Health == 0 then 
				gameisactive = false
				print("Game has Ended!")
				Announcement.Value = Giant.Name.. " Has been Slayed!"
                -- This is when the script should return to the top and restart
			end
		end
	end
end```

this isn’t how you use return?

I know. All that did was stop the script completely.

I guess just wrap the code you want to repeat in a function and run it when you want it to.

2 Likes

It looks like a break would work fine for getting out of the inner loop and starting over.

while true do
	if gameisactive == false then

		-- ...

		while wait() do
			if Giant.Character:WaitForChild("Humanoid").Health == 0 then 
				gameisactive = false
				print("Game has Ended!")
				Announcement.Value = Giant.Name.. " Has been Slayed!"
                break -- this goes back to the top of `while true do`
			end
		end
	end
end

I don’t think you’ll need the gameisactive flag at all.

1 Like

I was thinking of trying that after trying a return (for some reason)! Thank you!