Tool-Part Destroy System Problem

So this is my first post.
I’m creating a basic system that will allow you to destroy an object/objects with a certain name.
What I mean by destroy is UnAnchor it so the part falls to the ground.
I’ve already made my the tool, which is an axe, as well as the animations.
I’ve placed and named parts in Workspace to “Wood” so they could be destroyed on contact with the axe if the axe is being swung, however I’ve spent at least 2 hours for the past 3 days trying to figure out what I did wrong because I keep getting errors and I do not completely understand what they mean.
I set it up so that when the player equips the axe and inputs left click, the MouseDown value is set to true and if the axe touches the wood while that value is true it should UnAchor it.
I’m not sure if I have to use while loop to check again and again to see if the value is true or false.
The script is below and if possible please give me some tips as I’m a bit new to the scripting side of Roblox, thank you.

Hopefully I posted this in the right section and in the right format if not, my apologies.

I Honestly had no idea what to call this Topic, my apologies if you thought this was something else.

--Variables
local tool = script.Parent
local woodhit = false
local mousedown = false
local debounce = false
local breakt = false
local debounce = false

--Checks if mouse is down
tool.Equipped:Connect(function(Mouse)
	Mouse.Button1Down:Connect(function()
		print("Mouse Clicked")
		mousedown = true
	end)

--Checks if mouse is up
tool.UnEquipped:Connect(function(Mouse)
	Mouse.Button1Up:Connect(function()
	mousedown = false
end)
		
--Checks if wood is hit and mouse is down
script.Parent.Handle.Blade.Touched:Connect(function(hit)
			if mousedown == true then
				if not debounce then
					debounce = true
		
				print("Mouse down")
				if hit.Name == "Wood" then
					
						hit.Anchored = false
						wait(1)
						debounce = false
						end
				end
		end
			
end)

What’s the error?

I noticed that you don’t close the tool.Equipped event code. Once you create a function, you need to end it somewhere but here you don’t. You’d also need a ) to match the other parenthesis.

Same with tool.UnEquipped, you are missing a end)

error

I’ve looked over this error several times and I’ve checked the internet for answers, but I can’t figure it out.

Am I supposed to double end)?

Like this:
end)
end)

Yes, you need an end) for the .Equipped function, and for the Mouse.Button1Up function

It’s Exactly what I did, but it still doesn’t work and there’s no errors relating to it in the output.

local tool = script.Parent
local woodhit = false
local mousedown = false
local debounce = false
local breakt = false
local debounce = false


tool.Equipped:Connect(function(Mouse)
	Mouse.Button1Down:Connect(function()
		print("Mouse Clicked")
		mousedown = true
		end)
	end)
	
tool.Unequipped:Connect(function(Mouse)
	Mouse.Button1Up:Connect(function()
		print("Mouse released")
		mousedown = false
	end)
end)
		
script.Parent.Handle.Blade.Touched:Connect(function(hit)
	if mousedown == true then
		print("Mousedown")
				if not debounce then
					debounce = true
				if hit.Name == "Wood" then
					hit.Anchored = false
						wait(1)
						debounce = false
			end
		end			
	end			
end)

Just a quick tip, you should not use Mouse.Button1Down since it’s deprecated and because there’s no need to use it in your situation. Tool.Activated exists and fire when the player activates the tool (pressing mouse button 1 or the equivalent for the current device).

Based off what you said (if I understand it correctly), you simply want an axe to “cut down” wood/trees. I don’t see why you would need to detect if the player is holding down the mouse button. All you really want to detect is when the player clicks or activates the tool and than play the swing animation + allow hits to register while the swing animation is playing (or whatever period of time you want).

For example, something like this:

-- Config --
local HitPeriod = 4; -- The period of time after you click in which you are allowed to hit a part (Its prob best if you set this to the Animation.Length for your swing animation. That way you can only hit a part while the swing animation is playing)
local HitCooldown = 1; -- The cooldown period for when you hit a part. (If set to 0 than you can pretty much take out multiple trees/etc while swinging)
local SwingCooldown = 3; -- An optional cooldown for after you finish swinging. (That way you can't spam swing everytime your swing finishes) 
local AllowedParts = {"Wood", "Iron", "Poop"}; -- A list of parts that the axe can hit!

-- Variables --
local Tool = script.Parent;
local HitPart = Tool:WaitForChild("Handle");

local AllowHit = false; -- Debounce for HitPeriod
local ToolEquipped = false; -- This is to make sure that the tool is equipped

local SwingDebounce = false; -- Debounce for SwingCooldown
local HitDebounce = false; -- Debounce for HitCooldown

-- Tool Connections --
Tool.Activated:Connect(function() -- Fires whenever the tool is activated (if you aren't using a handle than make sure you disable the RequiresHandle property for the tool).
	if SwingDebounce then -- Just another "visual" aid for you.
		print("Swing is on cooldown!!");
	end;
	if not SwingDebounce then
		SwingDebounce = true;
		AllowHit = true;
-- Play the animation here blah blah
		HitPart.Color = Color3.fromRGB(85, 255, 127); -- Just a visual aid for you. So you can see when your hits are registered
		wait(HitPeriod); -- When you have your animation ready, its best that you set this to the animations length
		HitPart.Color = Color3.fromRGB(255, 255, 255); -- Same as before
		AllowHit = false;
		wait(SwingCooldown);
		SwingDebounce = false;
	end;
end);

Tool.Equipped:Connect(function(Mouse)
	ToolEquipped = true;
end);

Tool.Unequipped:Connect(function()
	ToolEquipped = false;
end);

-- Touch Connection --
HitPart.Touched:Connect(function(Hit)
	local ValidHit = table.find(AllowedParts, Hit.Name); -- Checks if Hit.Name is found in our part list(AllowedParts)
	if HitDebounce then -- Just another "visual" aid for you.
		print("Hit debounce is in effect. Cannot hit the part "..Hit.Name);
	end;
	if ToolEquipped and AllowHit and ValidHit and not HitDebounce and Hit.Anchored then
		HitDebounce = true;
		Hit.Anchored = false;
		wait(HitCooldown);
		HitDebounce = false;
	end;
end);

Setup:
SetupExample

Note: There may be typos, etc.

Also this is simply to give you a general idea. Make sure to add on or remove anything you like/dislike. I tried to make the code similar in function as your previous one (with a few minor differences).

If you for whatever reason really need to detect whether the player is holding down the mouse or not than I suggest using UserInputService with InputBegan and InputEnded.

You basically want to make a local script → detect whenever MouseButton1 is inputted → Set your MouseDown bool depending on which Input Connection is triggered (InputBegan = sets to true, InputEnded = sets to false) → Fire a remote telling the server that the player is holding down the button → Do all necessary sanity checks → Continue with your logic.

Anyway, thats just the general idea. Hopefully this helps and goodluck!

I did not know activated worked for mouse click as well I thought it was another form of function Equipped, thank you.

1 Like

I will give it a try, thank you.