I’m trying to store a trove for when a player joins into a cache, with connections. Then when they leave, defore deleting their cache, I want to destroy connection
self.Cache[player] = {
CurrentBoat = nil,
CurrentPets = {},
OwnedGamepasses = {}
}
local _Trove = Trove.new()
_Trove:Add(player:GetAttributeChangedSignal("Coins"):Connect(function()
self:UpdateLeaderstats(player)
end))
table.insert(self.Cache[player], _Trove) -- Store connections in cache
-- when player leaves attempt to index nil with 'Destroy
self.Cache[player].Trove:Destroy() -- Clear connections
Currently you are inserting a trove into your table without having a reference for it. Therefore, your cache dictionary would look something like this:
{
CurrentBoat = nil,
CurrentPets = {},
OwnedGamepasses = {},
[4] = { _objects = {} } -- this is your trove currently with no key
}
You want to add it into your table with it having a refrence [key] in the table. Therefore, you’d do something like this:
local trove = Trove.new()
local cache = self.Cache[player]
cache.Trove = trove -- See, now inside your table it'd have Trove = { _objects = {} }
-- player leaving
self.Cache[player].Trove:Destroy()
Also you are doing nothing wrong with how you are using Trove:Add(signal) but you can do something along the lines of this for any signal:
local trove = Trove.new()
-- Attribute changed signal example
trove:Connect(player:GetAttributeChangedSignal("Coins"), funciton()
self:UpdateLeaderstats(player)
end)
-- DescendantAdded example
trove:Connect(instance.DescendantAdded, function(child)
print("DescendantAdded ", child.Name)
end)
-- Humanoid Died exmaple:
trove:Connect(Humanoid.Died, function()
-- add some death effect to character
end)
No, they’ll be disconnected but, there’s still a [old] reference into that table so what you would do next is set the trove reference to nil as shown below:
-- This is the same way you'd do it for Maids, and ScriptSignals
self.Cache[player].Trove:Destroy()
self.Cache[player].Trove = nil
So, what happens when you use :Destroy() on a Trove is it will disconnect all events and your table _objects in that Trove will be empty when you print it out after destroying.
Here’s a screenshot of mine. The red outlined trove is me connecting to a child being added to workspace, and the blue outlined one is the trove after being destroyed.
Try using :Connect() instead and see how that works for you. The :Construct() function can be used on objects such as; Instances, and tables / classes that contain events or instances, but I believe it’s not for a single signal such as Activated
local Signal = require(somewhere.Signal)
-- All of these are identical:
local s = trove:Construct(Signal)
local s = trove:Construct(Signal.new)
local s = trove:Construct(function() return Signal.new() end)
local s = trove:Add(Signal.new())
-- Even Roblox instances can be created:
local part = trove:Construct(Instance, "Part")