Help with axe script

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I would like my axe to detect when there is a tree and chop it down.
  2. What is the issue? Include screenshots / videos if possible!
    The script keeps outputting No Tree even after detecting the tree.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried looking up any scripts on the devforum but there aren’t any scripts about axes.
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

Here is the code

local Tool = script.Parent
local Handle = Tool.Blade
local Animation = Tool.Animation
debounce = false
local damage = 1

function checkTree()
	Handle.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Trunk") then
			if debounce == false then
				debounce = true
				print("Tree Detected")
				local hits = hit.Parent.Hit
				
				hits.Value += 1

				wait(1)
				debounce = false
			else
				print("No Tree")
				wait(1)
			end
		end
	end)
end

Tool.Activated:Connect(function()
	checkTree()
end)

Each tree has a trunk and 2 values.

2 Likes

You should try ray casting inside the axe. Depending on how big the axe is, I believe you should set the pivot point to the axe blade for this to work better. But here’s what you do:

local rayParams = RaycastParams.new()
rayParams.FilterDescendantsInstances = {workspace.Map.Trees} --your trees folder
rayParams.FilterType = Enum.RaycastFilterType.WhiteList --this makes it where you ONLY want to detect trees

function checkTree()
  local ray = workspace:RayCast(Handle.Position,Vector3.new(0,0,0),rayParams) --the reason you put a blank Vector3 is because that's the direction of the ray cast. What you want to do is ray cast in one spot.
  if ray then --If it doesn't detect any trees this will be nil. If it does detect a tree, "ray" will become a table full of all the information about what it hit
    local treePart = ray.Instance
    local tree = treePart.Parent
    --the rest of your code
  end
end

Also, the pivot properties look like this:
image

1 Like
local Tool = script.Parent
local Handle = Tool.Blade
local Animation = Tool.Animation
debounce = false
local damage = 1

function checkTree()
	Handle.Touched:Connect(function(hit)

		local t = hit.Parent:FindFirstChild("Trunk") then
if t then
			if not debounce then
				debounce = true
				print("Tree Detected")
				local hits = hit.Parent.Hit
				
				hits.Value += 1

				wait(1)
				debounce = false
			else
				print("No Tree")
				wait(1)
			end
		end
	end)
end

Tool.Activated:Connect(function()
	checkTree()
end)

Worth a try but most likely no difference.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.