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

How Would I Use Module Scripts With Tables?

Asked by
Aepeus 59
6 years ago
Edited 6 years ago

I have multiple scripts that require suffixes, and I always have to edit each and every single one of them so I decided to use module scripts. The problem is I never used them? Also do module scripts require functions?

Module Script

local module = {}
function module:getSuf(suf)
local suffixes = {
    'k','M','B','T','qd','Qn','sx','Sp','O',
    'N','de','Ud','DD','tdD','qdD','QnD','sxD',
    'SpD','OcD','NvD','Vgn'
}
module:getSuf(suffixes)
end
return module

Script

local rs = game:WaitForChild("ReplicatedStorage")
local events = rs:WaitForChild("Events")

game.Players.PlayerAdded:connect(function(plr)
    local module = require(game.ReplicatedStorage.Suffixes)
    module.getSuf(suf)

    local leaderstats = plr:WaitForChild("leaderstats")
    local stats = plr:WaitForChild("stats")

    local robux = stats.Robux
    local tix = stats.Tix

    local robuxstring = leaderstats.Robux
    local tixstring = leaderstats.Tix   

    robux.Changed:connect(function()

    for i = #suffixes,1, -1 do
        if robux.Value >= 10 ^ (i * 3) then
            robuxstring.Value = math.floor(robux.Value / 10 ^ ((i*3) - 2)) * 0.01 .. suffixes[i] .. "+"
            break
        elseif robux.Value <= 10 ^ 3 then
            robuxstring.Value = math.floor(robux.Value * 100) * 0.01
            break
        end
    end
end)

    tix.Changed:connect(function()

    for i = #suffixes,1, -1 do
        if tix.Value >= 10 ^ (i * 3) then
            tixstring.Value = math.floor(tix.Value / 10 ^ ((i*3) - 2)) * 0.01 .. suffixes[i] .. "+"
            break
        elseif tix.Value <= 10 ^ 3 then
            tixstring.Value = math.floor(tix.Value * 100) * 0.01
            break
        end
    end
end)

end)

Also my studio crashes.

0
Er, can you reword your question? It's currently hard to understand what you're asking. antonio6643 426 — 6y
0
I have lots of scripts like the script below which requires the table in the module script. Aepeus 59 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

You can just instantly return the suffixes table value upon requireing like so:

return {
    'k','M','B','T','qd','Qn','sx','Sp','O',
    'N','de','Ud','DD','tdD','qdD','QnD','sxD',
    'SpD','OcD','NvD','Vgn'
}

This means when the module is required, it'll just return that array. To use this in your script, you can just do it like so:

local rs = game:WaitForChild("ReplicatedStorage")
local events = rs:WaitForChild("Events")

game.Players.PlayerAdded:connect(function(plr)
    local suffixes = require(game.ReplicatedStorage.Suffixes)

    local leaderstats = plr:WaitForChild("leaderstats")
    local stats = plr:WaitForChild("stats")

    local robux = stats.Robux
    local tix = stats.Tix

    local robuxstring = leaderstats.Robux
    local tixstring = leaderstats.Tix   

    robux.Changed:connect(function()

    for i = #suffixes,1, -1 do
        if robux.Value >= 10 ^ (i * 3) then
            robuxstring.Value = math.floor(robux.Value / 10 ^ ((i*3) - 2)) * 0.01 .. suffixes[i] .. "+"
            break
        elseif robux.Value <= 10 ^ 3 then
            robuxstring.Value = math.floor(robux.Value * 100) * 0.01
            break
        end
    end
end)

    tix.Changed:connect(function()

    for i = #suffixes,1, -1 do
        if tix.Value >= 10 ^ (i * 3) then
            tixstring.Value = math.floor(tix.Value / 10 ^ ((i*3) - 2)) * 0.01 .. suffixes[i] .. "+"
            break
        elseif tix.Value <= 10 ^ 3 then
            tixstring.Value = math.floor(tix.Value * 100) * 0.01
            break
        end
    end
end)

end)

Hopefully this solves your problem, if it does, please accept it as the answer, if not let me know by commenting below!

0
Thanks Ill accept this answer although i solved after a couple minutes. Aepeus 59 — 6y
Ad

Answer this question