Help with Disconnect()

I’m am creating a timer for my game Course Adventures. When the script comes to Disconnect(), it does not stop the function, but rather continues to repeat itself. Do you think you might know what the problem is:

playerService.PlayerAdded:Connect(function(player)
	startEvent.OnServerEvent:Connect(function(player)
		if debounce.Value == false and isFinished.Value == false then 
			local t = os.clock()	
			local timer;
			local function timerFunction() -- the time function
				if stillInGame.Value == true then -- if the player is still in game, then keep running
					local timePassed = os.clock() - t		
					if timePassed <  900  and isFinished.Value == true -- if the player finished the course, then stop the time					 
                        timer:Disconnect()					
					elseif timePassed >= 900 then -- if the player is taking to long, then teleport him back to the main place
						teleportService:Teleport(player.UserId, 4831581791)
						timer:Disconnect()
					end
				else -- if the player isnt in the game, then stop the time
					timer:Disconnect()
				end
			end
			timer = runService.Heartbeat:Connect(timerFunction)
		end
	end)
end)

This is the basic structure of my script. I did not include the entire script.

If you would like to see the whole script, comment below.

Have a good day!

Have you tried some basic debugging to see if the line is being run where the Disconnect function is mentioned?

you’re missing a then next to isFinished.Value == true
this should be giving you an error though instead of running, otherwise, the code looks good to me

maybe you’re firing the remote to the server too often? or not setting isFinished to false?

1 Like

If all your debugging fails, try setting the connection to nil as well and only run the function if it’s not nil, just as an extra check. For example:

local connection = nil

connection = game:GetService("RunService").Heartbeat:Connect(function()
	if connection then
		print("1")
		connection:Disconnect()
		connection = nil
	end
end)

-- 1
2 Likes

I forgot to inlcude then in the script above, my bad.

I also forgot to include debounce.Value = true after the first conditional line. So if the remote’s being fired too much, this debounce will stop it from running several times.

This was the issue. I never set its value to false! Thanks!

I do have one question though. When you call disconnect(), is it supposed to stop the function right away or run everything else in the function, then stop. The reason why I’m asking this is because sometimes my script prints a string after I called disconnect.

local timer 
local function timerFunction()
     if debounce = true then
         print("Disconnect")
         timer:Disconnect()
         print("Would this be printed") -- Would it print this?
     end
end

timer = runservice.Heartbeat:Connect(timerFunction)

Have a good day.

1 Like

no, Disconnect doesn’t stop the function, it stops the connection between the function and the event, so after disconnecting, if the event happens again, it won’t trigger the function anymore

2 Likes