Currently I have this script for an axe that allows it to chop down trees as well as deal damage to humanoids or players of the opposite team.
So far the script works however I am also planning to reuse this script for other different tools such as a pickaxe, and many other ones that i plan to add in the future too. I find that reusing the script and changing its variables and functions each time for a different tool is quite tedious to do, so I am wondering if there is a more simple way to make the process less complicated and most importantly less tedious.
I'm thinking of using module scripts however I am not really familiar with using them, mostly because I'm having trouble with finding out the best way to set them up and what parts of the script can be used in a module and what parts that don't.
Tool = script.Parent
local character = script.Parent.Parent.Parent.Character
local coolDown1 = 0.3
local coolDown2 = 0.3
local swinging = false
local canSwing = true
local damage = 26
---------------------------------------------------------------------------------------------------------------------
Tool.Activated:connect(function()
if canSwing == false then return end
canSwing = false
local character = Tool.Parent
local humanoid = character.Humanoid
local animation = humanoid:LoadAnimation(script.Parent.Animation)
animation:Play(0.4,1,1)
wait(0.3)
animation:AdjustSpeed(0)
wait(0.2)
animation:AdjustSpeed(2)
Tool.Handle.Swoosh:Play()
swinging = true
wait(0.3)
swinging = false
wait(coolDown1) --animation cool down time
canSwing = true
end)
---------------------------------------------------------------------------------------------------------------------
local canDamage = true
canChop = true
Tool.Handle.Touched:connect(function(hit)
if swinging == false then return end
if canDamage == false or canChop == false then return end
local function whenHit()
canDamage = false
if hit.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
local user = game.Players:GetPlayerFromCharacter(Tool.Parent)
if not player then
hit.Parent.Humanoid:TakeDamage(damage)
end
if player then
if player.TeamColor == user.TeamColor then
hit.Parent.Humanoid:TakeDamage(0)
else
hit.Parent.Humanoid:TakeDamage(damage)
end
end
end
Tool.Handle.FleshHit:Play()
wait(coolDown2)
canDamage = true
end
local function chop()
canChop = false
Tool.Handle.Chop:Play()
local chopValue = hit.Parent.ChopValue
if hit and hit.Parent:FindFirstChild("ChopValue") then
if chopValue.Value >= 1 then --if the tree is still choppable then decrease the chopValue
local amount = 1
chopValue.Value = chopValue.Value - amount
local function checkValue() --checks to see if chopValue is less than or equal to 0, if it is then down the tree
--print(chopValue.Value)
if chopValue.Value <= 0 then --if the tree can be downed then
local tree = chopValue.Parent
local function checkSize()
if tree.TreeSize.Value == 1 then
local drop = game.ServerStorage.Drops.Trees.TreeDropSmall
local dropClone = drop:Clone()
dropClone.Parent = game.Workspace
dropClone:MoveTo(tree.Trunk.Position)
elseif tree.TreeSize.Value == 2 then
local drop = game.ServerStorage.Drops.Trees.TreeDropMedium
local dropClone = drop:Clone()
dropClone.Parent = game.Workspace
dropClone:MoveTo(tree.Trunk.Position)
elseif tree.TreeSize.Value == 3 then
local drop = game.ServerStorage.Drops.Trees.TreeDropBig
local dropClone = drop:Clone()
dropClone.Parent = game.Workspace
dropClone:MoveTo(tree.Trunk.Position)
end
end
local function timber() -- Responsible for downing the tree
checkSize()
tree.Leaves.Anchored = false
tree.Trunk:Destroy()
end
timber()
end
end
checkValue()
end
end
wait(coolDown2)
canChop = true
end
if hit and hit.Parent:FindFirstChild("Humanoid") then --if hit humanoid then
whenHit()
elseif hit and hit.Parent:FindFirstChild("ChopValue") then --if hit tree then
chop()
end
end)
Quick explanation: When the script detects a part with the required IntValues in it or it's parent, it will execute the chop function which treats the part like a tree that can be chopped down.

3 comments
Well, you could have all of the different functions for each tool in one script, and then define a string variable (local toolType = "Axe") then have the script decide which function to do based on the variable. Then, when you wanna make a new tool, just copy this script to the new tool and change the toolType variable to be "Pickaxe" or "Sword" or any other tools you want, as long is that one var
Alternatively, you could put all the functions in a module script and call them based on the toolType variable.
hmm I think that could actually work, imma go test it out