[SOLVED] Need Help for Wood Cutting Script!

Hello, everyone.

I need a little help

This is a tree felling script. Which also works!

I can’t do that with the clickdetector and the tool.
The script should not be a localscript.
example

I’ve already read this through, I just don’t understand how to insert it!

Here is the code:

local Baum = script.Parent.Parent.Parent.Baum
local clickDetector = Baum.ClickDetector
local schlaege = 8
local debounce = true
local Toolname = "Wood Axe"
		

clickDetector.MouseClick:Connect(function(plr)
	if plr and plr.Character and plr.Character:FindFirstChild(Toolname) then
		
		if debounce == true then
			print("Schwing!")
			schlaege = schlaege - 1
			if schlaege == 6 then
				Baum.Leaves2.Transparency = 1
			end
			if schlaege == 3 then
				Baum.Leaves1.Transparency = 1
			end
			if schlaege == 0 then
				Baum.Transparency = 1
				clickDetector.MaxActivationDistance = 0
				Baum.CanCollide = false
				debounce = false
				wait(30)
				Baum.Transparency = 0
				Baum.Leaves1.Transparency = 0
				Baum.Leaves2.Transparency = 0
				schlaege = 8
				clickDetector.MaxActivationDistance = 32
				Baum.CanCollide = true
				debounce = true
			end
		end
	end
	
end)

Can you explain a little further. Is this within a tool itself, or do you want it to work whilst holding a tool?

If I hold the tool then it should work. If I don’t keep it, then don’t.

Ah, then you should detect it from the tool itself.

Click detectors do not detect clicks if a player is holding a tool.

1 Like

I’ve read that too, but how should I insert it.
I always get error messages.

What do you mean by insert it?

Add a localscript into the tool with the code.
In the script detect if the Hit is a log.
If so, fire a RemoteEvent, and then from there connect to it from a serverscript.

1 Like

Excuse me, I am dyslexic, come from Germany and used the google translate. I learn more when I see the code.

I understood that I should do the code in a localscript. But that’s exactly what I didn’t want!

I understand, but i dont think there is any other way without detecting it within the tool.

Can you show me the code how it works? Also with the local script?

Put a LocalScript inside the Tool, and in that script, use the Tool’s Activated connection to get when the player clicked using that tool. Then using LocalPlayer:GetMouse() you can get the mouse’s Target object that the player clicked on. Then if the target object was an object of the tree, then continue with your original tree cutting function.

2 Likes

Hey it Works with this Localscript in the Axe, Thanks for Dev_Ryan and majdTRM

local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer -- from a LocalScript
local mouse = localPlayer:GetMouse()
local schlaege = 8
local debounce = true
local Baum = workspace.Baum

mouse.Button1Down:Connect(function()
	if mouse.Target == Baum then
		
		if debounce == true then
			print("You hit me!")
			schlaege = schlaege - 1
			if schlaege == 6 then
				Baum.Leaves2.Transparency = 1
			end
			if schlaege == 3 then
				Baum.Leaves1.Transparency = 1
			end
			if schlaege == 0 then
				Baum.Transparency = 1
				Baum.CanCollide = false
				debounce = false
				wait(30)
				Baum.Transparency = 0
				Baum.Leaves1.Transparency = 0
				Baum.Leaves2.Transparency = 0
				schlaege = 8
				Baum.CanCollide = true
				debounce = true
			end
		end
		
	end
end)

But I have 2 more problems now.

  1. There are several trees and only the same one can be clicked, the others are ignored.

  2. It works even if the ax is in the backpack. It should only work if the ax is equipped. I’ve already tried it.

    local tool = Instance.new("Tool")
    tool.RequiresHandle = false
    tool.Parent = game.Players.LocalPlayer.Backpack
     
    function onActivation()
        print("Tool activated")
    end
     
    tool.Activated:Connect(onActivation)

But i Think it is wrong.

Try Tool.Equipped instead of Tool.Activated

1 Like

Thank it Works. But,
There are several trees and only the same one can be clicked, the others are ignored.

local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer -- from a LocalScript
local mouse = localPlayer:GetMouse()
local schlaege = 8
local debounce = true
local Baum = workspace.Baum
local Tool = script.Parent.Parent.WoodAxe
Tool.Equipped:Connect(function(mouse)
	mouse.Button1Down:Connect(function()
		if mouse.Target == Baum then
			if debounce == true then
				print("You hit me!")
				schlaege = schlaege - 1
				if schlaege == 6 then
					Baum.Leaves2.Transparency = 1
				end
				if schlaege == 3 then
					Baum.Leaves1.Transparency = 1
				end
				if schlaege == 0 then
					Baum.Transparency = 1
					Baum.CanCollide = false
					debounce = false
					wait(30)
					Baum.Transparency = 0
					Baum.Leaves1.Transparency = 0
					Baum.Leaves2.Transparency = 0	
					schlaege = 8	
					Baum.CanCollide = true	
					debounce = true	
				end
			end			
		end		
	end)
end)

The reason is, the Baum variable only represents one tree. Instead of checking if mouse.Target == Baum.

You should give each tree an identifiable tag.(See CollectionService (roblox.com) and Tag Editor - Roblox) And then check it using CollectionService:HasTag.

But for now you can simply check if the part is called “Baum”: mouse.Target.Name == "Baum"

Sry, but this is too difficult for me.

Is there maybe another solution?

Or would someone be so nice for me and do that with three trees so that I can see the code?
I can just upload what I have!

Sorry i didnt see your posts.

Why cants you just check if the name of the target is “Baum”?

mouse.Target.Name =="Baum"

That doesn’t work, I take down a tree and another shows the result.

Can you do this for me, please? I don’t get this with the (See CollectionService (roblox.com) and Tag Editor - Roblox) this is too hard for me! i dont understand it!

Okay, the reason it is affecting another tree, is because you are changing the properties of a avariable you already set.

Simply add this before if mouse.Target.Name == "Baum":

local Baum = mouse.target

Then show me your code.


For the second part, CollectionService helps you add tags to objects.

For example:

local CollectionService = game:GetService("CollectionService")

if CollectionService:HasTag(part, "Some Tag") then
  -- do somethig here
end

Basically, it checks if part has the tag “Some Tag”.

You can use
Tag Plugin to add these tags in studio, and check them with your script.

But it is not necessary

Hey, thanks for your Help. Is that right?

I have installed “Tag Editor” and is that right?
2

-- test
local CollectionService = game:GetService("CollectionService")
-- test
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer -- from a LocalScript
local mouse = localPlayer:GetMouse()
local schlaege = 8
local debounce = true
local Baum = mouse.target
local Tool = script.Parent.Parent.WoodAxe
-- test
if CollectionService:HasTag(Baum, "Some Tag") then
-- test
Tool.Equipped:Connect(function(mouse)
	mouse.Button1Down:Connect(function()
		if mouse.Target == "Baum" then
			if debounce == true then
				print("Du schlägst Baum!")
				schlaege = schlaege - 1
				if schlaege == 6 then
					Baum.Leaves2.Transparency = 1
				end
				if schlaege == 3 then
					Baum.Leaves1.Transparency = 1
				end
				if schlaege == 0 then
					Baum.Transparency = 1
					Baum.CanCollide = false
					debounce = false
					mouse.Target.respawn.Value = true
					print("Der Baum fällt!")

					wait(5)
					Baum.Transparency = 0
					Baum.Leaves1.Transparency = 0
					Baum.Leaves2.Transparency = 0	
					schlaege = 8	
					Baum.CanCollide = true	
					debounce = true	
				end
			end			
		end
	end)
	end)
end

It doesn’t work.

I’ll send you the rxbl
NewTreeHitSystem.rbxl (71,5 KB)