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
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.
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).
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 also sorry if I don’t respond!
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