#Disconnecting

1 messages · Page 1 of 1 (latest)

shut mauve
#

I have this code that uses RunService .Heartbeat:Connect that is inside a function, but the problem is, there is a check in the start that kinda works for just not doing everything else in the code. i want to know if this check is still being made for every frame? this check is going to run for every single time i call the function like if it was a task.spawn? and if so, how can i disconnect the function for it to stop checking? here is the code

run.Heartbeat:Connect(function()
        if not building then return end -- the check im talking about
        
        local mouse = uis:GetMouseLocation()
        local camray = camera:ViewportPointToRay(mouse.X, mouse.Y)
        
        local rayparams = RaycastParams.new()
        rayparams.FilterType = Enum.RaycastFilterType.Include
        rayparams.FilterDescendantsInstances = {workspace.Mapa.Tiles}
    
        local ray = workspace:Raycast(camray.Origin, camray.Direction * 10000, rayparams)
        
        if ray and ray.Instance then
            highlight.Parent = ray.Instance
            ghostpart:PivotTo(CFrame.new(ray.Instance.CFrame.Position + Vector3.new(0, ghostpart:GetExtentsSize().Y/2, 0)))
            
            currenttile = ray.Instance
        end
        
        if table.find(tilesplanted, currenttile) or ray == nil then
            highlight.FillColor = Color3.new(1,0,0)
            canput = false
        else
            highlight.FillColor = Color3.new(1,1,1)
            canput = true
        end
    end)
#

i discovered that it doesnt disconnects after that

#

i fixed by doing this but if someone has a better solution pls tell me

local connect = nil
    connect = run.Heartbeat:Connect(function()
        
        if building == false then connect:Disconnect() return end
        
        local mouse = uis:GetMouseLocation()
        local camray = camera:ViewportPointToRay(mouse.X, mouse.Y)
        
        local rayparams = RaycastParams.new()
        rayparams.FilterType = Enum.RaycastFilterType.Include
        rayparams.FilterDescendantsInstances = {workspace.Mapa.Tiles}
    
        local ray = workspace:Raycast(camray.Origin, camray.Direction * 10000, rayparams)
        
        if ray and ray.Instance then
            highlight.Parent = ray.Instance
            ghostpart:PivotTo(CFrame.new(ray.Instance.CFrame.Position + Vector3.new(0, ghostpart:GetExtentsSize().Y/2, 0)))
            
            currenttile = ray.Instance
        end
        
        if table.find(tilesplanted, currenttile) or ray == nil then
            highlight.FillColor = Color3.new(1,0,0)
            canput = false
        else
            highlight.FillColor = Color3.new(1,1,1)
            canput = true
        end
    end)
azure mason
#

u wanna check if its still running?

shut mauve
#

i know it still runs after the return(running the check). i wanted to make it stop running after the return

#

so i could avoid memory leak and lag

azure mason
#

i mean it shouldnt for sure but theres like

#

connect.connected

#

or smth]

#

property