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

Trying to make something only happen when a tool is equipped, not working?

So I've been trying to make a tree chopping down system that only runs when the player equips a tool. Here's what the script looks like so far. (im very very new to scripting)

local part = script.Parent 
local detector = script.Parent.ClickDetector
local hits = 0
local hitsn = math.random(2, 5)
local leaves = part.Parent.Leaves
local branch = Instance.new("Part")
local playerName = game.Players.LocalPlayer.Name
local axe = game.Players.LocalPlayer.Backpack.Axe

detector.MouseClick:Connect(function(player)
	if axe.Parent == playerName then
		if hits == hitsn then
			print("tree has been chopped!")
			hits = 0
			part.Anchored = false
			leaves.Anchored = false
			wait(2)
			part:Destroy()
			leaves:Destroy()
			branch.Anchored = false
			branch.Parent = workspace.Items
		else
			print("not yet!")
			hits = hits + 1
		end	
	end	
end)

i think it could be an issue with line 8, and how i assigned the axe but i really don't know. just so you know, im not using the default roblox inventory, im using an inventory system that i found on the toolbox that i customised a bit.

1 answer

This script detects when the player equips the tool in his hand, you can try to use it to do what you want.

local player = game.Players.LocalPlayer

local Tool = player.Backpack:FindFirstChild("NameTool") or player.Character:FindFirstChild("NameTool")

Tool.Equipped:Connect(function(mouse)
	
	print("the tool is equipped") --This function will only be activated when the player equips the tool 
	
end)

1 comment

0

I forgot to say this is a Local script inside the StarterGui folder

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