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

Stopping a function when goal is met?

Hello! Today I was making code for a minigame I was going to make. I've got the main basics set already, it's just when I want the round to end, I can't stop the function. This makes it continue for 600 seconds when the last person is alive. Any tips? I tried everything before going on the website, I really didn't want to waste any of your guy's time.

local roundLength = 600
local intermissionLength = 10
local InRound = game.ReplicatedStorage.InRound
local GameAreaSpawn = game.Workspace.GameAreaSpawn
local SpawnArea = game.Workspace.SpawnArea
local Status = game.ReplicatedStorage.Status

InRound.Changed:Connect(function()
	if InRound.Value == true then
		for _, player in pairs(game.Players:GetChildren()) do
			local char = player.Character
			char.HumanoidRootPart.CFrame = GameAreaSpawn.CFrame
		end
	else
		for _, player in pairs(game.Players:GetChildren()) do
			local char = player.Character
			char.HumanoidRootPart.CFrame = SpawnArea.CFrame
		end
	end
	end)

local function roundTimer()
	while wait() do
		for i = intermissionLength, 1, -1 do
			InRound.Value = false
			wait(1)
			Status.Value = "Intermission: ".. i .." seconds left"
		end
		for i = roundLength, 1, -1 do
			InRound.Value = true
			wait(1)
			Status.Value = "Game: ".. i .." second left!"
			
		end
	end
end
spawn(roundTimer)

game:GetService('Players').PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			if player.Team == game.Teams.PlayersTeam then
				player.Team = game.Teams.ViewersTeam
				local playeramount = game.Teams.PlayersTeam:GetPlayers()
				if #playeramount == 1 then
					print"You WON!!!!"
					wait(3)
					--Right here is where i need the round timer to stop and go back to intermission
			end
		end
		end)
	end)
end)



2 comments

0

My brain is mush, can you specify which function you want to stop?

0

Oh my god I accidently left some left over code in it! I fixed it, sorry for the miscommunication. I want the roundTimer function to restart. On line 48 is where a restart script would go for the function, I just don't know what to use to restart it.

1 answer

I actually figured it out, sorry for the request! Heres a snippet of the code that worked incase anyone skims over this thread when its ended. Works like a charm :)

		for i = roundLength, 1, -1 do
			InRound.Value = true
			wait(1)
			Status.Value = "Game: ".. i .." second left!"
			local playeramount = game.Teams.PlayersTeam:GetPlayers()
			if #playeramount == 1 then
				wait(4)
				--INSERT FOR WHEN PLAYER WINS
				break
			else
				print(#playeramount)
				end

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