Hello, I have a tool and I want to server script get which player is holding the tool. (Must work too when the tool is not parented to player)
I dont have much to say about it.
Hello, I have a tool and I want to server script get which player is holding the tool. (Must work too when the tool is not parented to player)
I dont have much to say about it.
You can get the parent of the tool. For example, if the player is holding the tool you would do…
local Tool = --Variable Here
local character = Tool.Parent
local player = game:GetService("Players"):GetPlayerFromCharacter(character)
For the backpack you would do…
local Tool = --Variable Here
local player = Tool.Parent.Parent --Backpack parent
local character = player.Character
When a tool is being held it is parented to the character. Do what @SilentSuprion said to find a specific tool or if you want to know what tool their holding do this:
local tool
for _, v in pairs(character:GetChildren()) do
if not v:IsA("Tool") then continue end
tool = v
return
end
Use Character:FindFirstChildOfClass("Tool")
instead, as a character can only have a single tool equipped at any given moment anyway.
Forgot about that, you could also do Character:FindFirstChildWhichIsA("Tool")
No class inherits from the ‘Tool’ class so ‘WhichIsA()’ would be redundant.
Character:FindFirstChildWhichIsA("BackpackItem")
‘BackpackItem’ is the base class for tools, hopperbins etc.