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

How would I go about and make a Module Script for this code?

Asked by 4 years ago

I have several LocalScripts that have very very similar code. I am trying to learn how to use module scripts for the first time and I need some help. I tried looking a some tutorials, but I didn't make any sense of it.

Code:

local db = true
local player = game.Players.LocalPlayer
local target = game.Workspace.Misc.TPfloors.TPsec7 -- this and
local delay = 2

script.Parent.MouseButton1Click:Connect(function()
    if db == true then
        db = false
        player.Character.HumanoidRootPart.CFrame = target.CFrame * CFrame.new(0, 8, 0)
        for i = delay, 1, -1 do
            script.Parent.Text = i
            wait(1)
        end
        script.Parent.Text = "Section 7" -- this are the only strings that are changed between the scripts
        db = true
    end
end)

Please let me know if you have advice!

0
What part of this code did you want to put into a module script? the text that is returned? (i.e. "Section 7") if so, itd help to clarify a bit as to how you want the module to return the text (based on a keyword, the button's name, etc) SerpentineKing 3885 — 4y
0
So basically, I need it so I can just use require() in a couple different local scripts if that makes any sense, but I also need it so where the text says the right thing for the right button. Hope this helps some? Tyler090130 619 — 4y
0
i just edited my answer to explain your needs User#23252 26 — 4y
0
Responding to your comment, i've adjust the scripts in my response, which should better suit what you are trying to accomplish SerpentineKing 3885 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Improvements

Use WaitForChild() to make sure an instance exists before using it

Use GetService() for the Players Service

Use workspace instead of game.Workspace

Use SetPrimaryPartCFrame to move the player's Character model instead of moving the PrimaryPart only

Use Activated for GuiButtons in order for it to work on all devices and not just computers

Explanation

In the below you can see that I first make a local variable for the module which "requires" the module (Line 3)

Later, when determining the button's Text, I use a function ive defined in the module and send in the button's name as a parameter

In the module script, I make a table dictating the index (which matches my button's name ("Button1") and give this position in the table the value ["Section 7"]

When the function is called, I return this value, which is returned with the module which had previously been "required"

Notice, module scripts only run when you use "require" so it will not run when the server is created like LocalScripts or Server Scripts

Local Script

local player = game:GetService("Players").LocalPlayer
local target = workspace:WaitForChild("Misc"):WaitForChild("TPfloors"):WaitForChild("TPsec7")
local module = require(game:GetService("ReplicatedStorage"):WaitForChild("StringList"))
local button = script.Parent

local db = true
local delay = 2

button.Activated:Connect(function()
    if db == true then
        db = false
        player.Character:SetPrimaryPartCFrame(target.CFrame * CFrame.new(0, 8, 0))
        for i = delay, 1, -1 do
            button.Text = i
            wait(1)
        end
        button.Text = module.StringReturn(button.Name)
        db = true
    end
end)

Module Script

local module = {}

function module.StringReturn(button)
    local stbl = {["Button1"] = "Section 7"}
    return(stbl[button])
end

return(module)

EDIT

Running the same function is similar to changing the button's text, except we'll keep it in the module (it will act like a local function)

Local Script

local player = game:GetService("Players").LocalPlayer
local module = require(game:GetService("ReplicatedStorage"):WaitForChild("StringList"))
local button = script.Parent
local db = true

button.Activated:Connect(function()
    if db == true then
        db = false
        db = module.StringReturn(button, player)
    end
end)

Module Script

local module = {}

local target = workspace:WaitForChild("Misc"):WaitForChild("TPfloors"):WaitForChild("TPsec7")
local delay = 2

function module.StringReturn(button, player)
    local stbl = {["Button1"] = "Section 7"}
    player.Character:SetPrimaryPartCFrame(target.CFrame * CFrame.new(0, 8, 0))
    for i = delay, 1, -1 do
        button.Text = i
        wait(1)
    end
    button.Text = stbl[button.Name]
    return(true)
end

return(module)
0
I really do appreciate the response, but what I'm looking for is the complete opposite. I wanted to use require() in a couple different local scripts so I don't have this same exact code withing 9 different LocalScripts. I wasn't looking for just requiring a module just to change a string. I'm not very good at explaining things, so bare with me. Tyler090130 619 — 4y
Ad

Answer this question