Disconnect renderstepped

Hi, i am trying to make an obby where you have 3-4 attempts but if you use all your attempts you have to wait 2 minutes, i am using os.clock() so i can use Minute:Seconds format, problem is i dont know anything about it.

I have 3 issues,

  1. Multiple surfaceGuis cloning into playerGui – there should be 1, there is 3
  2. At the end, it starts going to the negative
  3. I don’t know how to disconnect renderstepped

(if there is a more efficient way to do this; You are more than welcome to share)

Code:

--//Variables
local Counter = 0
local ObbySpawn = workspace.Spawns:WaitForChild("ObbySpawner")
local ObbyBlock = workspace:WaitForChild("Map"):WaitForChild("ObbyBlocker")
local Part = script.Parent
local DefaultSpawnLocation = workspace:WaitForChild("Spawns"):WaitForChild("DefaultSpawnLocation")

local SurfaceGui = script:WaitForChild("SurfaceGui")
local plr = game:GetService("Players").LocalPlayer
local plrGui = plr:WaitForChild("PlayerGui")

local deadline = os.clock() + 120 -- 2 Minutes

local debounce = false

--//Functions

function UpdateTimer()
	local TimerLabel = plrGui:WaitForChild("Timer").TextLabel
	local timerFormat = "%d:%d" -- As long it has the "%d", it will be possible to replace them with numbers.
	-- Assuming this is inside of a BindToRenderStep or Updater.
	local remaining = deadline - os.clock()
	local m = math.floor(remaining/60) -- Minutes
	local s = math.floor(remaining%60) -- Seconds -- Milliseconds

	TimerLabel.Text = "Retry in: " .. string.format(timerFormat,m,s)
end

function onTouched(Hit)
	if debounce == false then
		debounce = true
		if Hit.Parent:FindFirstChild("Humanoid") then
		local Char = Hit.Parent
		if Counter <= 3  then
			Char:MoveTo(ObbySpawn.Position + Vector3.new(0,5,0))
			Counter += 1
		elseif Counter > 3 then
			Char:MoveTo(DefaultSpawnLocation.Position + Vector3.new(0,5,0))
			ObbyBlock.Transparency = 0.5
			ObbyBlock.CanCollide = true
			
			local SurfaceGuiClone = SurfaceGui:Clone()
			SurfaceGuiClone.Name = "Timer"
			SurfaceGuiClone.Parent = plrGui
			SurfaceGuiClone.Adornee = ObbyBlock
			
			game:GetService("RunService").RenderStepped:Connect(UpdateTimer) -- Connect to RenderStepped so we can update every frame.
			
			task.wait(120)
			--disconnect renderstepped
			-- I DONT KNOW HOW
			--destroy gui
			SurfaceGuiClone:Destroy()
			
			ObbyBlock.Transparency = 1
			ObbyBlock.CanCollide = false
			
			Counter = 0
		end
	end
end

Part.Touched:Connect(onTouched)
6 Likes

You can use a connection. For example:

--//Variables
local Counter = 0
local ObbySpawn = workspace.Spawns:WaitForChild("ObbySpawner")
local ObbyBlock = workspace:WaitForChild("Map"):WaitForChild("ObbyBlocker")
local Part = script.Parent
local DefaultSpawnLocation = workspace:WaitForChild("Spawns"):WaitForChild("DefaultSpawnLocation")

local connection

local SurfaceGui = script:WaitForChild("SurfaceGui")
local plr = game:GetService("Players").LocalPlayer
local plrGui = plr:WaitForChild("PlayerGui")

local deadline = os.clock() + 120 -- 2 Minutes

local debounce = false

--//Functions

function UpdateTimer()
	local TimerLabel = plrGui:WaitForChild("Timer").TextLabel
	local timerFormat = "%d:%d" -- As long it has the "%d", it will be possible to replace them with numbers.
	-- Assuming this is inside of a BindToRenderStep or Updater.
	local remaining = deadline - os.clock()
	local m = math.floor(remaining/60) -- Minutes
	local s = math.floor(remaining%60) -- Seconds -- Milliseconds

	TimerLabel.Text = "Retry in: " .. string.format(timerFormat,m,s)
end

function onTouched(Hit)
	if debounce == false then
		debounce = true
		if Hit.Parent:FindFirstChild("Humanoid") then
			local Char = Hit.Parent
			if Counter <= 3  then
				Char:MoveTo(ObbySpawn.Position + Vector3.new(0,5,0))
				Counter += 1
			elseif Counter > 3 then
				Char:MoveTo(DefaultSpawnLocation.Position + Vector3.new(0,5,0))
				ObbyBlock.Transparency = 0.5
				ObbyBlock.CanCollide = true

				local SurfaceGuiClone = SurfaceGui:Clone()
				SurfaceGuiClone.Name = "Timer"
				SurfaceGuiClone.Parent = plrGui
				SurfaceGuiClone.Adornee = ObbyBlock

				connection = game:GetService("RunService").RenderStepped:Connect(UpdateTimer) -- Connect to RenderStepped so we can update every frame.

				task.wait(120)
				--disconnect renderstepped
				connection:Disconnect()
				--destroy gui
				SurfaceGuiClone:Destroy()

				ObbyBlock.Transparency = 1
				ObbyBlock.CanCollide = false

				Counter = 0
			end
			task.wait()
			debounce = false
		end
	end
	if Hit.Parent:FindFirstChild("Humanoid") then
		local Char = Hit.Parent
		if Counter <= 3  then
			Char:MoveTo(ObbySpawn.Position + Vector3.new(0,5,0))
			Counter += 1
		elseif Counter > 3 then
			Char:MoveTo(DefaultSpawnLocation.Position + Vector3.new(0,5,0))
			ObbyBlock.Transparency = 0.5
			ObbyBlock.CanCollide = true

			local SurfaceGuiClone = SurfaceGui:Clone()
			SurfaceGuiClone.Name = "Timer"
			SurfaceGuiClone.Parent = plrGui
			SurfaceGuiClone.Adornee = ObbyBlock

			game:GetService("RunService").RenderStepped:Connect(UpdateTimer) -- Connect to RenderStepped so we can update every frame.

			task.wait(120)
			--disconnect renderstepped
			-- I DONT KNOW HOW
			--destroy gui
			SurfaceGuiClone:Destroy()

			ObbyBlock.Transparency = 1
			ObbyBlock.CanCollide = false

			Counter = 0
		end
	end
end

Part.Touched:Connect(onTouched)
4 Likes

As you can see, I putted a connection. The connection was nil at first but then we have made connection a “RBXSignalConnection”. Then we can just put “:Disconnect”

Thank you,
Karilbol :smile:

2 Likes

thats one problem solved, i now have another issue, at the end it goes into the negatives, and if you die again it will continue from that number
image

3 Likes

Can you show me where the timer counts, please? I can’t find it.

Also, sorry for late response :wink: :worried:

3 Likes
function UpdateTimer()
	local TimerLabel = plrGui:WaitForChild("Timer").TextLabel
	local timerFormat = "%d:%d" -- As long it has the "%d", it will be possible to replace them with numbers.
	-- Assuming this is inside of a BindToRenderStep or Updater.
	local remaining = deadline - os.clock()
	local m = math.floor(remaining/60) -- Minutes
	local s = math.floor(remaining%60) -- Seconds -- Milliseconds

	TimerLabel.Text = "Retry in: " .. string.format(timerFormat,m,s)
end
2 Likes

You can remove the “-” (if exists):
(Idk if this is gonna work but try it)

TimerLabel.Text = "Retry in: " .. string.format(timerFormat,m,s)

if TimerLabel.Text:find("-") then
TimerLabel.Text = TimerLabel:gsub("-","")
end

I think you can remove the “if” function.

You can also only do this:

TimerLabel.Text = "Retry in: " .. string.format(timerFormat,m,s)

TimerLabel.Text = TimerLabel:gsub("-","")
2 Likes

Im not on Studio, so I can’t confirm this is correct

2 Likes

Did my post work? It looks like someone already solved your problem with the negative
:smile: But thank you for wanting help! I always love to help! :wink:

2 Likes

sorry for late response! i got help from someone else but you helped a lot! good day/night

2 Likes

Thank you!

Have a good day/night too! :smile:

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.