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.

001Tool = script.Parent
002local character = script.Parent.Parent.Parent.Character
003local coolDown1 = 0.3
004local coolDown2 = 0.3
005local swinging = false
006local canSwing = true
007local damage = 26
008 
009---------------------------------------------------------------------------------------------------------------------
010 
011Tool.Activated:connect(function()
012    if canSwing == false then return end
013    canSwing = false
014    local character = Tool.Parent
015    local humanoid = character.Humanoid
View all 118 lines...

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 =

1local 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

1local tab = {}
2tab.AxeDamage = 3000000
3tab.AxecoolDown1 = 0.5
4tab.AxecoolDown2 = 1000000
5tab.PickaxeDamage = -200
6tab.PickaxecoolDown2 = 0.2
7tab.PickaxecoolDown1 = 0.3
8return tab

something along those lines.

Then in your pickaxe script you could do something like

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

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

01--modulescript
02local tab{}
03tab.Pickaxe = {1, 0.2, 0.3}
04return tab
05 
06--localscript
07local damageModule = require(game.Workspace.Whereveritis.DamageModule)
08local cd1 = damageModule[script.Parent.Name][2]
09local cd2 = damageModule[script.Parent.Name][3]
10local damage = damageModule[script.Parent.Name][1]

Hope I helped!

Ad

Answer this question