Hello
If I understand correctly, although the Backpack is situated under the player object, it is created/deleted together with the Character object?
So if I need to initialize it (add/remove items from it in a script), I must do it in CharacterAdded event (on Server) or in StarterCharacterScripts (on client), because in PlayerAdded event or in StarterPlayerScripts the Backpack may not yet exist?
Is this correct?
Thank you
My problem is the following
In server script in OnPlayerAdded event I add some items to the player backpack.
In a Local script in StarterCharacterScripts, I need to use these items. However I do not know how to detect in the Local script that the backpack has finished replicating to the client, so I can start to use it.
I tried to set an attribute on the player with value true when OnPlayerAdded finishes adding items in the player backpack and then I listen for a change of this attribute in the Local script. However, although the Local script detects the change of the attribute, it seems that this attribute replicates before the Backpack itself, so this does not solve the problem…
If I put something like task.wait(0.1) in the Local script, then it works, but I do not really like this…
The Backpack container itself is replicated to the client. I need to know when its children (the Tools) has finished replicating.
Will WaitForChild (“Backpack”) wait also for the children of the Backpack before returning result?
I do not know which are these children, they are added by the server. I need my LocalScript to continue when the full content of the backpack (all tools added by the server) has finished replicating
local Cache = {} --Cache tools that have already been seen.
local function OnChildAdded(Child)
if not (Child:IsA("Tool")) then return end --Ignore non-tool instances.
if Cache[Child] then return end --Check if tool has already been seen.
Cache[Child] = true --Assign tool to cache.
--Do code.
end
Backpack.ChildAdded:Connect(OnChildAdded)
for _, Child in ipairs(Backpack:GetChildren()) do
OnChildAdded(Child)
end
You can listen to/handle the backpack’s ChildAdded event/signal.
repeat
Backpack.ChildAdded:Wait()
until #Backpack:GetChildren() == 10 --Wait for the backpack to contain 10 tools before continuing the script's execution.