Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I detect what tool LocalPlayer is holding using a script?

I want to detect what tool LocalPlayer is holding using a script. I made a script, already. But, it does not work. It is a local script in StarterPlayer.StarterCharacterScripts.

Here is the script:

local descendants = game.Players.LocalPlayer.Backpack:GetDescendants()
for index, descendant in pairs(descendants) do
		while true do
			wait(0.1)
			print(descendant.Name)
			wait(0.1)
			print(descendant.Name)
			wait(0.1)
			print(descendant.Name)
		wait(0.1)
	end
end

2 comments

0

change name to Name

0

It still doesnt work, @GamePlayer365247v2

1 answer

✓ Accepted answer

If you want to get a tool that player is holding then you need to get it from Character:

local Player = game.Players.LocalPlayer

local findtool = function()
	local tool = Player.Character:FindFirstChildWhichIsA("Tool")
	if tool then
		return tool
	end
end

local tool = findtool()
print(tool.Name)

-- prints tool name

EDITED:

local Player = game.Players.LocalPlayer

local GetTool = function(toolname)
	local found = nil
	local tools = Player.Backpack:GetChildren()
	local holdingtool = Player.Character:FindFirstChildWhichIsA("Tool")
	for i,v in pairs(tools) do
		if v.Name == toolname then
			found = v
		end
	end
	if holdingtool then
		if holdingtool.Name == toolname then
			found = holdingtool
		end
	end
	if found ~= nil then
		return found
	end
end



Player:WaitForChild("Backpack").ChildAdded:Connect(function()
	local tool = GetTool("Tool") --Insert name of tool
	if tool then
		print(tool.Name)
	end
end)

Player.Character.ChildAdded:Connect(function()
	local tool = GetTool("Tool") --Insert name of tool
	if tool then
		print(tool.Name)
	end
end)




2 comments

0

What is the location of the script?

0

LocalScript in starterplayerscripts I would assume

This discussion is preserved as it was. New answers, comments, and votes are closed.