How to add Disconnect Function?

I want to add a disconnect function so it would be less laggy but I have no idea how to do it and how to achieve this.
I have found a solution in stunhandler v2 but I tried to replicate but it didn’t work.

Also, I can’t add a wait function or it will lead to this (image)

local PlayerEditor = {}

--services
local heartbeat = game:GetService("RunService").Heartbeat

local ischecking = false
local checkconnection

local function Checker(variety:string,original:ValueBase,Duration:number,PastTime:number,Character)

	local futuretime = Duration + PastTime
	if os.time() == futuretime then
		ischecking = true
		
		local hum = Character:WaitForChild("Humanoid")
		hum.WalkSpeed = original
		
	end

end



function PlayerEditor.changespeed(Char,WalkSpeed,Duration:number?)
	local hum = Char:WaitForChild("Humanoid")

	local oldwalkspeed = hum.WalkSpeed

	hum.WalkSpeed = WalkSpeed

	if Duration ~= nil then
		local currentime = os.time()
		local checkconnection = heartbeat:Connect(function()Checker(oldwalkspeed,Duration,currentime,Char) end)
	
	end
end

“pls forgive my spaghetti code”

btw the function i want to disconnect is checkconnection

Then just do this:

if checkconnection then
   checkconnection:Disconnect()
end

wait ok i will try and where do i add it?

You can just add it whenever you want to disconnect it.

it doesn’t disconnect


plus help its red

Could you tell me what error it’s giving me in the output?

wait it doesn’t but when you remove the if statement it give this error
ServerScriptService.PlayerEditor:17: attempt to index nil with ‘Disconnect’

The if statement needs to be there to check if the connection is even there in the first place, otherwise you would get the error you mentioned.

You’re setting your “checkconnection” variable here. But you’re trying to check if it exists in another function (without setting it). It’s probably giving that error because you didn’t assign the variable to anything and you’re attempting to utilize it.

Oh do you have any solution on how it will be fix any solution?

oh ok i see so there isnnt the connection in the first place

I’d say try setting the variable to nil (initially NOT in the function). Then, if the connection doesn’t exist it just won’t run (and it won’t spit out that error).

Line 7

local checkconnection = nil

ok i will try it out in the script

1 Like

ok it doesn’t spit out the error but it doesn’t disconnect the function

That’s because it’s assigned to nil, you need to actually call your other function which sets it to heartbeat in order to disconnect it (the if checkconnection thenbasically checks if that variable isn’t nil or false).

sorry if this is kinda confusing :sweat_smile: also sorry if I don’t respond!

It’s okay I understand most of it

local checkconnection
local checkconnection = heartbeat:Connect(function()Checker(oldwalkspeed,Duration,currentime,Char) end)

i think accidentally found the problem why it wasnt working
it because i put local twice
it should be this

local checkconnection
checkconnection = heartbeat:Connect(function()Checker(oldwalkspeed,Duration,currentime,Char) end)

thank you for helping me realize that

so it should be
so the error is cause by rewriting the local variable
i am quite dumb

local PlayerEditor = {}

--services
local heartbeat = game:GetService("RunService").Heartbeat

local ischecking = false
local checkconnection

local function Checker(variety:string,original:ValueBase,Duration:number,PastTime:number,Character)

	local futuretime = Duration + PastTime
	if os.time() == futuretime then
		ischecking = true
		
		local hum = Character:WaitForChild("Humanoid")
		hum.WalkSpeed = original
		
	end

end



function PlayerEditor.changespeed(Char,WalkSpeed,Duration:number?)
	local hum = Char:WaitForChild("Humanoid")

	local oldwalkspeed = hum.WalkSpeed

	hum.WalkSpeed = WalkSpeed

	if Duration ~= nil then
		currentime = os.time()
		checkconnection = heartbeat:Connect(function()Checker(oldwalkspeed,Duration,currentime,Char) end)
	
	end
end

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