Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is there any way to make this script less tedious to write?

Asked by
Duksten 20
7 years ago

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.

0
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 Br4veh3art23 46 — 7y
0
Alternatively, you could put all the functions in a module script and call them based on the toolType variable. Br4veh3art23 46 — 7y
0
hmm I think that could actually work, imma go test it out Duksten 20 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago

You could add an intvalue inside each weapon to determine how much damage it does (and the other variables like cooldown too) then you could then make the variables =

local damage = script.Parent.Damage.Value

This would make the script load that damage and you would not have to edit it repeatedly.

Modulescripts are easy to use and probably a better solution.

You would make a modulescript with the text

local tab = {}
tab.AxeDamage = 3000000
tab.AxecoolDown1 = 0.5
tab.AxecoolDown2 = 1000000
tab.PickaxeDamage = -200
tab.PickaxecoolDown2 = 0.2
tab.PickaxecoolDown1 = 0.3
return tab

something along those lines.

Then in your pickaxe script you could do something like

local Tool = script.Parent
local character = game.Players.LocalPlayer
local damageModule = require(game.ReplicatedStorage.DamageModule) --basically wherever it is in the game
local coolDown1 = damageModule[Tool.Name.."coolDown1"]
local coolDown2 = damageModule[Tool.Name.."coolDown2"]
local swinging = false
local canSwing = true
local damage = damageModule[Tool.Name.."Damage"]

^ that's what I do with my games. Other people probably prefer a single table per object like so

--modulescript
local tab{}
tab.Pickaxe = {1, 0.2, 0.3}
return tab

--localscript
local damageModule = require(game.Workspace.Whereveritis.DamageModule)
local cd1 = damageModule[script.Parent.Name][2]
local cd2 = damageModule[script.Parent.Name][3]
local damage = damageModule[script.Parent.Name][1]

Hope I helped!

Ad

Answer this question