Is there an event to detect when player picks up a tool from the floor into their Backpack? I tried Backpack.ChildAdded but switching tools will trigger it too.
You could use a .Touched event perhaps.
Keep track of what tool they’re holding. If the player was holding the tool before it went into their backpack, then they didn’t just receive the tool. Bad solution don’t use
This could also depend more on the nature of your game and how receiving/picking up tools works. If there’s any more info that could help decide the best way to go about this, I would edit your post to include it.
I guess I’ll have to go with Touched event, thanks.
Using ChildAdded you’re going to get more than tools that are just picked up from the 3D world (StarterPack, scripts parenting directly to Backpack, etc). Are you only looking for tools picked up from the 3D world, or tools added to the backpack in general? I wouldn’t recommend Touched if you’re looking for tools added in general, as it won’t account for the StarterPack items/etc.
In my situation, only tools picked up from workspace. But I would like a better solution in case for future development.
Problem I will need to solve are:
- Picking up tool while not equipping will not add the tool to Backpack, instead to character.
- Switching tools swaps the tool’s Parent between character and Backpack.
I recommend using a delay when you trigger the .Touched event. This stops the script from repeating every touch.
Hadn’t considered tools being put into your character when picked up. In this case, use a table to keep track of all the tools a player has. When a tool enters their backpack or character, check if it isn’t already in the table. When a tool enters the player’s backpack or character and isn’t tracked already, insert it into the table, then run the function.
Make sure to keep track of tools they no longer have too by removing them from the table when they lost them.
If you want any tool obtained by the character for future proofing, you can use ChildAdded/ChildRemoved with some tweaks:
ToolPickupDropDemo.rbxl (27.1 KB)
Sword is in StarterPack, Rocket Launcher is added to Backpack by script in ServerScriptService, and the Paintball Gun is picked up from the world. The localscript in StarterCharacterScripts prints when any of these are added/removed. If you want to run this from a location that doesn’t reset with the character (e.g. server script or StarterPlayerScripts), you’ll need to handle a new Backpack and Character being created every time the player spawns.
I used something like this.
Tool.Changed:Connect(function(Property)
if Property == "Parent" and Tool.Parent == workspace then return end
local Player
if Tool.Parent:IsA("Backpack") and Tool.Parent.Parent:IsA("Player") then -- IN BACKPACK
Player = Tool.Parent.Parent
elseif Tool.Parent:IsA("Model") and Players:GetPlayerFromCharacter(Tool.Parent) then -- EQUIPPED IN CHARACTER
Player = Players:GetPlayerFromCharacter(Tool.Parent)
end
if not Player then return end
print("PICKED UP BY",Player.Name)
end)
I just tested this, it works! Although, it fires every time the player equips or de equips the tool as well. Maybe you could deconnect the function if you needed it to only happen once?
I’ve found the solution, and it’s easier than ever. The local script would run every time a player picks up the tool.
This is all the code you need inside the local script, nothing else.
print("Tool picked up")
Yup, that works! Perfect solution. Both simple and cheap on processing power.