Using require() to access a ModuleScript causes the error attempt to index global 'Frame' (a nil value).
ClientMain.lua
-- Located in StarterGui
local TooltipsModule = require(ReplicatedStorage.ModuleScripts.TooltipsModule)
local PlacementSystemModule =
require(ReplicatedStorage.ModuleScripts.PlacementSystemModule)
TooltipsModule.CreateInterface(game.Players.LocalPlayer)
PlacementSystemModule.CreateInterface(game.Players.LocalPlayer)
PlacementSystemModule.lua
-- Located in ReplicatedStorage
local PlacementSystemModule = {}
local OutputModule = require(ReplicatedStorage.ModuleScripts.OutputModule)
local TooltipsModule = require(ReplicatedStorage.ModuleScripts.TooltipsModule)
TooltipsModule.AddTooltip("E", "BUILD")
game:GetService("UserInputService").InputEnded:Connect(function(InputObject, GameProcessedEvent)
if InputObject.KeyCode == Enum.KeyCode.E and not GameProcessedEvent then
if ScreenGui.Enabled == true and not InPlacingMode then
ScreenGui.Enabled = false
TooltipsModule.EditTooltip("E", "BUILD")
else
ScreenGui.Enabled = true
TooltipsModule.EditTooltip("E", "CANCEL")
end
end
end)
return PlacementSystemModule
TooltipsModule.lua
-- Located in ReplicatedStorage
local TooltipsModule = {}
function TooltipsModule.CreateInterface(Player)
ScreenGui = Instance.new("ScreenGui")
TooltipsBar = Instance.new("Frame")
Frame = Instance.new("Frame")
ScreenGui.Name = "TooltipsModule"
ScreenGui.DisplayOrder = 5
ScreenGui.Parent = Player:WaitForChild("PlayerGui")
ScreenGui.ZIndexBehavior = Enum.ZIndexBehavior.Sibling
TooltipsBar.Name = "TooltipsBar"
TooltipsBar.Parent = ScreenGui
TooltipsBar.BackgroundColor3 = Color3.new(0.180392, 0.180392, 0.180392)
TooltipsBar.Position = UDim2.new(0, 0, 1, -24)
TooltipsBar.Size = UDim2.new(1, 0, 0, 24)
Frame.Parent = TooltipsBar
Frame.BackgroundColor3 = Color3.new(0.180392, 0.180392, 0.180392)
Frame.BackgroundTransparency = 1
Frame.Position = UDim2.new(1, -10, 0, 0)
Frame.Size = UDim2.new(0, -300, 1, 0)
end
function TooltipsModule.AddTooltip(Key, Text)
if not Frame:FindFirstChild(Key) then
local n = TextLabel:Clone()
n.Name = Key
n.Text = "["..Key.."] "..Text
n.Size = UDim2.fromOffset(n.TextBounds.X, n.TextBounds.Y)
end
end
function TooltipsModule.EditTooltip(Key, Text)
if Frame:FindFirstChild(Key) then
Frame:FindFirstChild(Key).Text = "["..Frame:FindFirstChild(Key).Name.."] "..Text
Frame:FindFirstChild(Key).Size = UDim2.fromOffset(Frame:FindFirstChild(Key).TextBounds.X, Frame:FindFirstChild(Key).TextBounds.Y)
end
end
return TooltipsModule
Why is this happening? I have similar situations in the same game but it doesn’t throw this error.
-- ModuleScript 2
local ScreenGui = Instance.new("ScreenGui")
local Frame = Instance.new("Frame")
function Module2.CreateInterface(Player)
ScreenGui.Parent = Player.PlayerGui
Frame.Parent = Player.PlayerGui:FindFirstChild("ScreenGui")
end
function Module2.FunctionName(Parameters)
Frame.Size = UDim2.fromOffset(10, 10) -- error is here
end
@C_Sharper Please don’t use Pastebin. Just post the code here, making people open another link is an unnecessary hassle.
@shadow7692 The main issue is that you’re not creating the interface before you try to call the function. You can fix this by adding a check to see if the frame exists before trying to apply changes to it. For example:
local object = {}
function object:CreateInterface(Player)
self.Frame = Instance.new("Frame")
end
function object:FunctionName(Parameters)
if self.Frame then
self.Frame.Size = UDim2.fromOffset(10, 10)
else
print("Interface not found!")
self:CreateInterface()
self.Frame.Size = UDim2.fromOffset(10,10)
end
end
return object
The thing is, even if I comment out calling the function, the error is still thrown - commenting the require() line is the problem. I’ve tried adding wait()'s before parts of it but it still throws the error.
I’ve tried amending my code with this, however it doesn’t actually ever fire, giving the impression that Frame is never created in the first place even though it is.
Where are you creating the interface? If you’re doing it in another script, that script may be running after the one calling FunctionName. In that case, there wouldn’t be an interface when it’s called, which is why it wouldn’t know that there’s “already” a frame — there wouldn’t be one yet.
Also, what do you mean by it doesn’t actually ever fire?" Does the script print that there’s not an interface when you think there should be one?
Where do you require the first module script (the one that calls FunctionName)? If you call it on the server, then the frame will only exist for the client. Also, Frame won’t exist anymore — it would be self.Frame (or Module2.Frame, depending on which script you’re trying this in).