I’m working on a simple backpack system for my game. I did all my scripting/UI work in one place, then once I was finished with testing(testing showed everything was working fine) I moved everything over to my main game. Now, with no changes, nothing is working. The culprit from what I found is the ChildAdded function;
-- LocalScript, nothing gets printed and the UpdateBackpack() function isn't called
Player.Backpack.ChildAdded:Connect(function(Child)
print("Child added")
if Child:IsA("Tool") then
warn("Doing backpack child update")
UpdateBackpack()
end
end)
The tools(which are Class:Tool) are added by a Script in ServerScriptService to the player’s backpack 3 seconds after the player has spawned. Like I said in the title, the LocalScript runs with no errors, my last line of code in the LocalScript is a print(), - this does print. I’m not sure what’s wrong? Any help would be much appreciated. Thanks.
My only idea is to add some delays to the local script, just to make sure - the output doesn’t print any errors, right?
local backpack = Player:WaitForChild("Backpack")
repeat wait() until Player.Backpack
backpack.ChildAdded:Connect(function(Child)
print("Child added")
if Child:IsA("Tool") then
warn("Doing backpack child update")
UpdateBackpack()
end
end)
No errors are printed, thanks for the advise though it seems even with an added a delay it doesn’t fix the problem. Like I said in the post, everything works fine in a differant place- just not my main game.
The LocalScript is in a Gui element. It loads as soon as the player loads in, it’s given about ~20 to load as the player has to go through a menu to spawn in. There are WaitForChild functions in my script but they’re set to time out at 2 seconds of waiting- and if they don’t find what they’re looking for an error will get outputted.
What did you define the player as? LocalPlayer? Do you have it connected to an event? Maybe the problem lies there, if you could supply the Player variable that would be easier.
First off, print (Child.Name), just to make sure it’s getting the Child Added correctly, also, maybe the UpdatedBackpack() function might not be working right?
Conventionally backpack related scripts always loop over existing items as well as handling added items:
function onChildAdded(child)
...
end
for _, child in ipairs(Player.Backpack:GetChildren()) do
onChildAdded(child)
end
Player.Backpack.ChildAdded:Connect(onChildAdded)
Thanks for all the replies, but I figured out the problem. One of my other LocalScripts had turned off the backpack using SetCoreGuiEnabled. It seemed when I turned it back on the problem was solved. I was not aware this is what that function did, I thought it solely removed the GUI of the backpack. Once I turned the SetCoreGuiEnabled backpack back on, everything worked.