Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 2 years ago
Edited 2 years ago

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

Log in to vote
0
Answered by 2 years ago

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)
0
I forgot to say this is a Local script inside the StarterGui folder horacioMiquen 15 — 2y
Ad

Answer this question