How would I call a previous Variable inside a ModuleScript

Normally you would call a variable like as

--// LocalScript
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer

But instead it would be inside of a ModuleScript, so I wouldnt need to copy and paste the same variables each time I create a LocalScript

Is this even possible or should I scrap this idea?


Before

--// ModuleScript
local Variables = {
	--// LocalPlayer
	["Players"] = game:GetService("Players"),
	["LocalPlayer"] = game:GetService("Players").LocalPlayer,
	["Character"] = game:GetService("Players").LocalPlayer.Character,
	["Humanoid"] = game:GetService("Players").LocalPlayer.Character:WaitForChild("Humanoid"),
	["CurrentCamera"] = workspace.CurrentCamera,
	["Mouse"] = game:GetService("Players").LocalPlayer:GetMouse(),
	
	--// Services
	["ContextActionService"] = game:GetService("ContextActionService"),
	["UserInputService"] = game:GetService("UserInputService"),
	["RunService"] = game:GetService("RunService"),
	["MarketplaceService"] = game:GetService("MarketplaceService"),
	["TweenService"] = game:GetService("TweenService"),
	["GuiService"] = game:GetService("GuiService"),

	--// Workspace
	["Lighting"] = game:GetService("Lighting"),
	["ReplicatedStorage"] = game:GetService("ReplicatedStorage"),
	["ReplicatedFirst"] = game:GetService("ReplicatedFirst"),
	["MaterialService"] = game:GetService("MaterialService"),
	["StarterPack"] = game:GetService("StarterPack"),
	["StarterPlayer"] = game:GetService("StarterPlayer"),
	["StarterGui"] = game:GetService("StarterGui"),
	["Teams"] = game:GetService("Teams"),
	["SoundService"] = game:GetService("SoundService"),
	
	["PlayerGui"] = game:GetService("Players").LocalPlayer.PlayerGui,
	["ScreenGui"] = game:GetService("Players").LocalPlayer.PlayerGui.ScreenGui,
}
return Variables

After

--// ModuleScript
local Variables = {
	--// LocalPlayer
	["Players"] = game:GetService("Players"),
	["LocalPlayer"] = Players.LocalPlayer,
	["Character"] = LocalPlayer.Character,
	["Humanoid"] = Character:WaitForChild("Humanoid"),
	["CurrentCamera"] = workspace.CurrentCamera,
	["Mouse"] = LocalPlayer:GetMouse(),
	
	--// Services
	["ContextActionService"] = game:GetService("ContextActionService"),
	["UserInputService"] = game:GetService("UserInputService"),
	["RunService"] = game:GetService("RunService"),
	["MarketplaceService"] = game:GetService("MarketplaceService"),
	["TweenService"] = game:GetService("TweenService"),
	["GuiService"] = game:GetService("GuiService"),

	--// Workspace
	["Lighting"] = game:GetService("Lighting"),
	["ReplicatedStorage"] = game:GetService("ReplicatedStorage"),
	["ReplicatedFirst"] = game:GetService("ReplicatedFirst"),
	["MaterialService"] = game:GetService("MaterialService"),
	["StarterPack"] = game:GetService("StarterPack"),
	["StarterPlayer"] = game:GetService("StarterPlayer"),
	["StarterGui"] = game:GetService("StarterGui"),
	["Teams"] = game:GetService("Teams"),
	["SoundService"] = game:GetService("SoundService"),
	
	["PlayerGui"] = LocalPlayer.PlayerGui,
	["ScreenGui"] = PlayerGui.ScreenGui
}
return Variables

Unfortunately you cannot do that. You would have to completely define the table first before adding more keys that reference existing keys in the table. For example:

local Variables = {
    --// LocalPlayer
    ["Players"] = game:GetService("Players"),

    --- other stuff
}

Variables.LocalPlayer = Variables.Players.LocalPlayer
Variables.Character = Variables.LocalPlayer.Character
Variables.Humanoid = Variables.Character:WaitForChild("Humanoid")
-- Rest of the variables

So should that mean ModuleScript would be viewed as

local Variables = {
	["Players"] = game:GetService("Players"),
	["currentCamera"] = workspace.CurrentCamera,
	
	--// Services
	["ContextActionService"] = game:GetService("ContextActionService"),
	["UserInputService"] = game:GetService("UserInputService"),
	["RunService"] = game:GetService("RunService"),
	["MarketplaceService"] = game:GetService("MarketplaceService"),
	["TweenService"] = game:GetService("TweenService"),
	["GuiService"] = game:GetService("GuiService"),

	--// Workspace
	["Lighting"] = game:GetService("Lighting"),
	["ReplicatedStorage"] = game:GetService("ReplicatedStorage"),
	["ReplicatedFirst"] = game:GetService("ReplicatedFirst"),
	["MaterialService"] = game:GetService("MaterialService"),
	["StarterPack"] = game:GetService("StarterPack"),
	["StarterPlayer"] = game:GetService("StarterPlayer"),
	["StarterGui"] = game:GetService("StarterGui"),
	["Teams"] = game:GetService("Teams"),
	["SoundService"] = game:GetService("SoundService"),
}

Variables.LocalPlayer = Variables.Players.LocalPlayer
Variables.Character = Variables.LocalPlayer.Character
Variables.Humanoid = Variables.Character:WaitForChild("Humanoid")
Variables.Mouse = Variables.LocalPlayer:GetMouse()
Variables.PlayerGui = Variables.LocalPlayer.PlayerGui
Variables.ScreenGui = Variables.PlayerGui.ScreenGui

return Variables

Why wouldn’t it be possible?

It might just be a hassle copy and pasting require(path) every script, and it might hurt readability a tiny bit, but I have seen other developers do stuff similar (specifically HD Admin) by having the framework of their game use .__index and _G/shared to set services if it hasn’t already yet be set, although I’m not too sure how you could do that with variables…

Yea that works. You can also just define the variables you need at the very top before creating the table. That way there’s no issue referencing what you need, and you can create the table all in one go. It really comes down to preference honestly.

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
-- Other variables you need

local Variables = {
    Players = Players,
    LocalPlayer = LocalPlayer,
    -- ...
}

return Variables
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.