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

How can I only use one Module Script for tools?

Asked by 2 years ago
Edited 2 years ago

So I have a modulescript called "Properties", this includes the name and description, etc. But the question is how can I only make one for tools?

This is example of the Properties modulescript.

return {
    MinDamage = 0,
    MaxDamage = 0,
    Name = "Test Sword",
    Description = "",
    CustomEffect = false,
    CustomEffectFunction = function()

    end
}

This is how it looks of a folder with full of tools

This is problematic because for example you want to put another variable in there, and you have like 30 tools, then you need to paste in it for each one.

How can I do this?

(Sorry for my english.)

0
Metatables. echobloxia 21 — 2y

1 answer

Log in to vote
0
Answered by
enes223 327 Moderation Voter
2 years ago

You can make a modulescript and use a table to create new tool tables which you can read the fields from, example code:

local Tool = {}
local defaultValues = {["MinDamage"] = 0; ["MaxDamage"] = 0; ["Name"] = ""; ["CustomEffect"] = false, ["CustomEffectFunction"] = function() end}

Tool.new = function(_MinDamage, _MaxDamage, _Name, _Description, _CustomEffect, _CustomEffectFunction)
    -- Create a new table
    local newTool = {}

    -- Initialiaze the fields, if the argument is nil use the default value
    newTool.MinDamage = _MinDamage or defaultValues.MinDamage
    newTool.MaxDamage = _MaxDamage or defaultValues.MaxDamage
    newTool.Name = _Name or defaultValues.Name
    newTool.Description = _Description or defaultValues.Description
    newTool.CustomEffect = _CustomEffect or defaultValues.CustomEffect
    newTool.CustomEffectFunction = _CustomEffectFunction or defaultValues.CustomEffectFunction

    return newTool
end

An example using it:

local ToolModule = require(whatever.Tool)
local Tool = ToolModule.new(10, 15, "Baseball Bat", false, nil) -- Custom effect function will be just empty function because it's default value is

print("Tool name: ", Tool.Name, "\n", "Tool damage: ", Tool.MaxDamage)

If you want you can go further with metatables like encapsulation and other stuff but it's better for you to start with basics and not burn yourself out, try to learn from the resources and not just copy and paste them.

Ad

Answer this question