I have this pretty simple script which is supposed to disconnect the connections it makes, but it is really inconsistent and doesn’t disconnect them most of the time.
local Player = MainFrame:GetPlayer()
local Tool = script:FindFirstAncestorOfClass("Tool")
local Mouse = Player:GetMouse()
local RemoteEvent = script.Parent:WaitForChild("RemoteEvent")
local debounce = false
local holding = false
local auto = true
local Connections: {[number]: RBXScriptConnection} = setmetatable({}, {__mode = "kv"})
local MouseConnections: {[number]: RBXScriptConnection} = setmetatable({}, {__mode = "kv"})
table.insert(Connections,
Tool.Equipped:Connect(function()
print("equipped", MouseConnections)
holding = false
table.insert(MouseConnections,
Mouse.Button1Down:Connect(function()
print("down")
holding = true
while holding do
if debounce then break end
debounce = true
warn(debounce)
RemoteEvent:FireServer()
task.wait(1/40)
debounce = false
if not auto then break end
end
end)
)
table.insert(MouseConnections,
Mouse.Button1Up:Connect(function()
print("up")
holding = false
end)
)
print(MouseConnections)
end)
)
table.insert(Connections,
Tool.Unequipped:Connect(function() --// ** the part that disconnects stuff (doesn't really work)
print("unequipped", MouseConnections)
for i, Connection in pairs(MouseConnections) do
print(Connection)
Connection:Disconnect()
print(Connection.Connected)
Connection = nil
MouseConnections[i] = nil
end
--table.clear(MouseConnections)
print(MouseConnections)
end)
)
table.insert(Connections,
Tool.Destroying:Connect(function()
task.spawn(function()
for i, Connection in pairs(Connections) do
print(Connection)
Connection:Disconnect()
Connection = nil
Connections[i] = nil
end
table.clear(MouseConnections)
table.clear(Connections)
MouseConnections = nil
Connections = nil
Player, Tool, Mouse, RemoteEvent, debounce, holding = nil, nil, nil, nil, nil, nil
end)
end)
)
Even when I unequip the item, it continues to function in the background as if nothing happened.
This is what unequipping the item should always output:
This is what is outputted most of the time:
I’ve been trying to debug this for the past hour (my entire output gets bombarded with information every time I interact with the item lol), and still have 0 clue on how to fix this issue.
Anything?