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!
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)