Need help specifying what a tool can hit

So, I want to specify who the axe can hit. I am using transparent dummy’s placed inside a tree model to detect hits and to display damage. Right now the axe damages all humanoids. Is it possible to specify who the axe can damage using a name based system? is there a better solution to all of this? In the future, I want to add rocks and different types of trees, but an axe will only be able to damage trees not rocks. As you make new and better axes you will be able to hit new trees.

here is the code for my axe:

local tool = script.Parent
local canDamage = false

local function onTouch(otherPart)

    local humanoid = otherPart.Parent:FindFirstChild("Humanoid")

    if not humanoid then 
        return 
    end

    if humanoid.Parent ~= tool.Parent and canDamage then
        humanoid:TakeDamage(5)
        print("Slashed")

    else
        return
    end

    canDamage = false
end

local function slash()

    local str = Instance.new("StringValue")
    str.Name = "toolanim"
    str.Value = "Slash" 
    str.Parent = tool
    canDamage = true
end

tool.Activated:Connect(slash)
tool.Handle.Touched:Connect(onTouch)

2 Likes

First of all, you might wanna install this plugin. This helps you create tags in the CollectionService. Now, you will have to make a new tag, name it “Trees” (or anything else you wish really), select all the humanoids that you wish to use as trees, and check the check mark for the trees tag. Now, make the following changes:

local CS = game:GetService("CollectionService")
local tool = script.Parent
local canDamage = false

local function onTouch(otherPart)

    local humanoid = otherPart.Parent:FindFirstChild("Humanoid")

    if not humanoid then 
        return 
    end

    if humanoid.Parent ~= tool.Parent and canDamage and CS:HasTag(humanoid,"Trees") then
        humanoid:TakeDamage(5)
        print("Slashed")

    else
        return
    end

    canDamage = false
end

local function slash()

    local str = Instance.new("StringValue")
    str.Name = "toolanim"
    str.Value = "Slash" 
    str.Parent = tool
    canDamage = true
end

tool.Activated:Connect(slash)
tool.Handle.Touched:Connect(onTouch)

Hope this helps! :grinning_face_with_smiling_eyes:

4 Likes