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 pause a function?

function Timer()

local Player = game.Players.LocalPlayer
local Label = Player.PlayerGui:WaitForChild("ScreenGui").TextLabel
local InGame = 0
while true do
    InGame = InGame+1
    if InGame < 10 then
        Label.Text = "0:0"..tostring(InGame)
    else if InGame < 60 then
        Label.Text = "0:"..tostring(InGame)
    else if InGame >= 60 then   
                local minutes = math.floor(InGame/60)
                local sec = InGame-minutes*60
                if sec < 10  then
                    Label.Text = tostring(minutes)..":0"..tostring(sec)
                else
                    Label.Text = tostring(minutes)..":"..tostring(sec)
                end
            end
        end
    end
    wait(1)
	end
	
end

game.Workspace.Activator.Touched:Connect(Timer)


game.Workspace.Deactivator.Touched:paused = true

`` I used game.Workspace.Deactivator.Touched:paused = true to try to pause the function, but it doesnt work. Can somebody help me?

1 comment

0

when do you want it to stop>

1 answer

✓ Accepted answer
-- You can't really pause a function. It will continue to run until the code inside of it is done. However you can just remove the function itself, run it in a loop, and detect if a variable is true or not before running the code.
local Player = game.Players.LocalPlayer
local Label = Player.PlayerGui:WaitForChild("ScreenGui").TextLabel
local InGame = 0
local activated = false
local RATE_PER_SECOND = 1

	game:GetService("RunService").Stepped:Connect(function(time,step)
	local increment = RATE_PER_SECOND * step
		if activated == true then
			InGame = InGame + increment 
			if InGame < 10 then
				Label.Text = "0:0"..tostring(InGame)
			elseif InGame < 60 then
				Label.Text = "0:"..tostring(InGame)
			elseif InGame >= 60 then   
				local minutes = math.floor(InGame/60)
				local sec = InGame-minutes*60
				if sec < 10  then
					Label.Text = tostring(minutes)..":0"..tostring(sec)
				else
					Label.Text = tostring(minutes)..":"..tostring(sec)
				end
			end
		end
	end)


game.Workspace.Activator.Touched:Connect(function()
	activated = true
end)

game.Workspace.Deactivator.Touched:Connect(function()
	activated = false
end)

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