What is the best way to get the player that ~~owns~~ possesses a tool?

Hello,

How would I get the player that owns has possession a tool, regardless of if it’s located in the backpack or on the player’s character? I’m not a big fan of getting the first ancestor which is a model and using Players:GetPlayerFromCharacter() because it seems to me like code smell but if this is the best way, then I guess I’ll bite the bullet, abstract it, and use it.

Thanks,
rad

Check from a BoolValue parented to the player when the player joins, and set it to true when the player first receives the tool. If you are saving the tool, then on the next time the player joins, check if the tool you want to look out for is saved in the Datastore, and create the BoolValue (assuming it’s when the player joins) and set it to true.

If the parent of the tool is a Backpack, the parent of the Backpack should be the player, unless you have Backpacks in unconventional places. If the parent of the tool is a Model, there is nothing wrong with using GetPlayerFromCharacter. If tools can be picked up off the ground, you’ll probably need this sort of check. On the other hand, if tools are given to players explicitly by server code, and can’t be dropped, you could save a map from Tool to Player and possibly have a convenient lookup, but this shouldn’t be a performance issue, you’d only do this if it had some organizational benefit. If what you meant by “owns the tool” is different from who currently has possession of it, then such a map would be practical, or you could use an IntValue object to tag the tool with the owner’s Player.UserId

2 Likes