Is there any way to make this script less tedious to write?
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.
002 | local character = script.Parent.Parent.Parent.Character |
005 | local swinging = false |
011 | Tool.Activated:connect( function () |
012 | if canSwing = = false then return end |
014 | local character = Tool.Parent |
015 | local humanoid = character.Humanoid |
016 | local animation = humanoid:LoadAnimation(script.Parent.Animation) |
017 | animation:Play( 0.4 , 1 , 1 ) |
019 | animation:AdjustSpeed( 0 ) |
021 | animation:AdjustSpeed( 2 ) |
022 | Tool.Handle.Swoosh:Play() |
032 | local canDamage = true |
034 | Tool.Handle.Touched:connect( function (hit) |
035 | if swinging = = false then return end |
036 | if canDamage = = false or canChop = = false then return end |
038 | local function whenHit() |
040 | if hit.Parent:FindFirstChild( "Humanoid" ) then |
041 | local player = game.Players:GetPlayerFromCharacter(hit.Parent) |
042 | local user = game.Players:GetPlayerFromCharacter(Tool.Parent) |
044 | hit.Parent.Humanoid:TakeDamage(damage) |
047 | if player.TeamColor = = user.TeamColor then |
048 | hit.Parent.Humanoid:TakeDamage( 0 ) |
050 | hit.Parent.Humanoid:TakeDamage(damage) |
054 | Tool.Handle.FleshHit:Play() |
059 | local function chop() |
061 | Tool.Handle.Chop:Play() |
062 | local chopValue = hit.Parent.ChopValue |
063 | if hit and hit.Parent:FindFirstChild( "ChopValue" ) then |
064 | if chopValue.Value > = 1 then |
066 | chopValue.Value = chopValue.Value - amount |
067 | local function checkValue() |
069 | if chopValue.Value < = 0 then |
071 | local tree = chopValue.Parent |
072 | local function checkSize() |
073 | if tree.TreeSize.Value = = 1 then |
075 | local drop = game.ServerStorage.Drops.Trees.TreeDropSmall |
076 | local dropClone = drop:Clone() |
077 | dropClone.Parent = game.Workspace |
078 | dropClone:MoveTo(tree.Trunk.Position) |
080 | elseif tree.TreeSize.Value = = 2 then |
082 | local drop = game.ServerStorage.Drops.Trees.TreeDropMedium |
083 | local dropClone = drop:Clone() |
084 | dropClone.Parent = game.Workspace |
085 | dropClone:MoveTo(tree.Trunk.Position) |
087 | elseif tree.TreeSize.Value = = 3 then |
089 | local drop = game.ServerStorage.Drops.Trees.TreeDropBig |
090 | local dropClone = drop:Clone() |
091 | dropClone.Parent = game.Workspace |
092 | dropClone:MoveTo(tree.Trunk.Position) |
095 | local function timber() |
098 | tree.Leaves.Anchored = false |
113 | if hit and hit.Parent:FindFirstChild( "Humanoid" ) then |
115 | elseif hit and hit.Parent:FindFirstChild( "ChopValue" ) then |
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.