So I’m attempting to make a system that includes a small snippet of code that detects new tools added to a Player’s backpack, the problem? .ChildAdded is not firing when picking up a new tool, and I was wondering if anyone knows any hacky solutions or reasons why this isn’t working? Keep in mind, the scope of the script is a server script running in ServerScriptService, if that is any help at all.
This may be because the tool is parented to the player’s character when the tool is equipped. Try something like this:
Player.Character.ChildAdded:Connect(function(childadded)
if childadded:IsA("Tool") then
-- the character equipped a tool.
end
end
Player.Backpack.ChildAdded:Connect(function(childadded)
if childadded:IsA("Tool") then
-- tool is in the player's backpack.
end
end
Apologies if i didnt understand properly, Or if the script doesn’t work/solve your problem.
Unfortunately it still doesn’t solve my problem, as, once I’ve unequipped the tool it will return to my backpack anyway, thus, should fire the ChildAdded event, yet, does not.
In that case, Could you show a snippet of your set up for detecting players? It may help me figure out the issue.
I’m using Roblox built-in PlayerAdded method to detect players added, and am assigning callback functions to Player.Backpack.ChildAdded()
I’m sorry if i have worded my other reply incorrectly, I am asking for a code block of how your set up is, There could be some sort of error in it.
Players.PlayerAdded:Connect(function(Player)
print(2)
weakReferenceBackpackTable[Player] = Player.Backpack
print(weakReferenceBackpackTable[Player])
weakReferenceBackpackTable[Player].ChildAdded:Connect(function(Child)
print(8)
if not CollectionService:HasTag(Child, "Gun") then
print(3)
return
end
print(4)
gunEquipEventTable[Player][Child] = {["Equip"] = Child.Equipped:Connect(function() print("Hi") end), ["Unequip"] = Child.Unequipped:Connect(function() print("Bye") end)}
print(5)
end)
end)
Essentially, I’m only ever getting “2” printed into the output, and even after having an item parented to the backpack, nothing gets printed past “2”
I don’t know your full set up, So i’m not sure if it specifically has to be that way, But im pretty sure this would be easier:
Players.PlayerAdded:Connect(function(Player)
print(2)
weakReferenceBackpackTable[Player] = Player.Backpack
print(weakReferenceBackpackTable[Player])
Player:WaitForChild("Backpack").ChildAdded:Connect(function(Child)
print(8)
if not CollectionService:HasTag(Child, "Gun") then
print(3)
return
end
print(4)
gunEquipEventTable[Player][Child] = {["Equip"] = Child.Equipped:Connect(function() print("Hi") end), ["Unequip"] = Child.Unequipped:Connect(function() print("Bye") end)}
print(5)
end)
end)
Also, Are you getting any errors?
Reading through, WaitForChild would offer no change to the code, as it’s a Server script, also weakReferenceBackpackTable[Player] is just a weak reference to the new player’s Backpack, so no difference in ChildAdded assignment either.
Additionally, I am not getting any errors. It’s just not firing the ChildAdded event.
Hmm, im pretty confused.
Also, just a few questions
Is the gun located in StarterPack?
Is the gun a Tool? or a model? or something else?
Are you not able to insert a script in the tool to detect the equip and unequip? or must it be in one server script?
The gun is in the workspace and I pick it up by walking over it.
It’s a tool, and I cannot use scripts in the tool as I need this system to be secure and scriptless internally, to reduce vulnerabilities.
Wait, So its not printing weakReferenceBackpackTable[Player]?
In this case, Check if your weakReferenceBackpackTable inserts players in when they are added, It won’t do it automatically (If you haven’t inserted players in the table, then add Table.Insert into the player added function)
I see, When you pick it up, Do you automatically equip it? If so, then maybe try this
Players.PlayerAdded:Connect(function(Player)
print(2)
weakReferenceBackpackTable[Player] = Player.Backpack
print(weakReferenceBackpackTable[Player])
Player.CharacterAdded:Connect(function(charactermodel)
charactermodel.ChildAdded:Connect(function(Child)
print(8)
if not CollectionService:HasTag(Child, "Gun") then
print(3)
return
end
print(4)
gunEquipEventTable[Player][Child] = {["Equip"] = Child.Equipped:Connect(function() print("Hi") end), ["Unequip"] = Child.Unequipped:Connect(function() print("Bye") end)}
print(5)
end)
end)
weakReferenceBackpackTable[Player].ChildAdded:Connect(function(Child)
print(8)
if not CollectionService:HasTag(Child, "Gun") then
print(3)
return
end
print(4)
gunEquipEventTable[Player][Child] = {["Equip"] = Child.Equipped:Connect(function() print("Hi") end), ["Unequip"] = Child.Unequipped:Connect(function() print("Bye") end)}
print(5)
end)
end)
This is basically all i can think of. Also, Like i said, Make sure you are inserting players into the table when they are added.
It also prints weakReferenceBackpackTable[Player] as a “backpack” instance. Even if I automatically equipped the item, when I unequip it, it should go back to the backpack and fire the ChildAdded event. The problem isn’t with the code, it’s with the fact that ChildAdded doesn’t fire on the backpack apparently.
After researching a bit more, It seems that the player’s backpack is deleted and a new one is made whenever a character respawns, therefore the weakReferenceBackpackTable[Player] is going to be nil when a player’s character respawns.
Hmm, Well it must be working then. Possibly try restarting studio, Or playtesting the game in a live server.
Also, I recommend you insert players into the table, rather than their backpack.
Also also, Is your character respawned before you pick the tool up or something like that? Sorry for not being helpful.
So I’m only storing the backpacks in the table temporarily so I have a reference point to the backpack, which I then connect the “ChildAdded” event to, the plan is the remove that backpack from the table, so the event still points to the instance, but I no longer store the instance, if that makes sense? Memory management stuff to keep it optimised and utilise as little memory on temporary variables as possible.
So I figured it out in the end, turns out I need to reassign the backpack each time the character is added, which works fine for me, everything else is working as expected, so that’s all good and fun
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.