So I’m creating a model with 2 parts in it on the server and calling AddTag to add the model to my collection service. I’m listening on the client for this to be added. However, when the client receives the signal it doesn’t detect that there’s 2 children in it, even when I added the model to the collection after parenting those parts to it.
Server:
local COLLECTION_SERVICE = game:GetService("CollectionService")
wait(3)
local model = Instance.new("Model")
local part1 = Instance.new("Part", model)
local part2 = Instance.new("Part", model)
model.Parent = workspace
COLLECTION_SERVICE:AddTag(model, "Test")
Client:
local COLLECTION_SERVICE = game:GetService("CollectionService")
COLLECTION_SERVICE:GetInstanceAddedSignal("Test"):Connect(function(obj)
print(obj.Name .. " added") --> Model added
print(#obj:GetChildren()) --> 0
end)
This is a quirk with general Roblox replication. I encountered the exact same issue while making a client-side model visibility system and it left me stuck for a month or so before I figured it out. Children aren’t guaranteed to replicate with their parents, you need to either wait for them to exist or commit actions on newly added children (or descendants).
Here’s some raw code right from my project demonstrating what I mean:
--- Determine if new trophies should be hidden based on user settings
-- @param object the trophy object
local function onTrophyAdded(object)
-- In-line connections are required for object access
object:GetAttributeChangedSignal(VISIBILITY_ATTRIBUTE):Connect(function ()
setTrophyVisibility(object, object:GetAttribute(VISIBILITY_ATTRIBUTE))
end)
object.DescendantAdded:Connect(function (newDescendant)
-- Forward the new descendant to get its visibility corrected
-- Class checking is done in the function itself, unnecessary to do here
setObjectVisibility(newDescendant, object:GetAttribute(VISIBILITY_ATTRIBUTE))
end)
if (DataValues.Options.ShowTrophiesSelf == false and object:IsDescendantOf(Character)) or (DataValues.Options.ShowTrophiesOthers == false and not object:IsDescendantOf(Character)) then
object:SetAttribute(VISIBILITY_ATTRIBUTE, false)
else
object:SetAttribute(VISIBILITY_ATTRIBUTE, true)
end
end
For context: setTrophyVisibility is just a for loop of setObjectVisibility across a model’s descendants and setObjectVisibility is a function that modifies the transparency based on the instance type:
onTrophyAdded is a function connected to a signal returned from GetInstanceAddedSignal on a tag which the server applies. The argument passed is a model (guaranteed by design of my code, there’s no room for unpredictability when assigning the tag).