Require() on ModuleScript causing issues

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.

Your not defining Frame anywhere…

1 Like

Even with local Frame before the functions the same error is thrown

1 Like

Assign “local Frame” to something. For example, a frame.

1 Like

Now it throws attempt to index upvalue 'Frame' (a nil value)

1 Like

Try replacing your module’s code with this, this might do the trick:

A little clarification, replace Module 2’s code with what I’ve written up.

1 Like

Unknown global 'self' when I try that

1 Like

I don’t think I did that right. Give me one second.

1 Like

Replace the entire code of Module 2 with this:

1 Like

Now it throws attempt to index field 'Frame' (a nil value)

1 Like

I see what’s going on. You’re calling “FunctionName” before “CreateInterface” Which is why the Frame field is never being created.

1 Like

What If you try to do this:

-- 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
1 Like

@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
1 Like

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.

1 Like

Do you ever call CreateInterface? You need to call that before FunctionName, otherwise the Frame field would never be made.

Ex:

Module2:CreateInterface()
Module2:FunctionName()
1 Like

I do call it before, actually, yes.

1 Like

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.

1 Like

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?

1 Like

The CreateInterface() is called from a client script. And I added:

if Frame then
    print("a")
end

Which should have printed a, but didn’t.

I also switched the order of them being called but that had no effect.

1 Like

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

1 Like