When is player's Backpack created/deleted?

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

2 Likes

no, the Backpack is a child of the “Player” Instance, PlayerAdded would work.

2 Likes

Backpack is in LocalPlayer Instance not Character Model

1 Like

I cannot manage it in PlayerAdded event, because each time when the player dies, the backpack will be deleted and recreated, no?

1 Like

NVM.

Char Limits

1 Like

No, in the documentation for Backpack is written:
"Once a character dies, the Backpack is removed and a new one will be created "

1 Like

what do you exactly need for your script?

1 Like

Created when CharacterAdded is fired and destroyed when CharacterRemoving is fired.

3 Likes

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…

Character:WaitForChild("Backpack")

To wait for the backpack to load/replicate on the client’s side.

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?

No, you’ll need to wait for those children individully as well.

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.

In that case fire a RemoteEvent to the client indicating what tools need to load/replicate for them.

1 Like