Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Module Script with global objects?

Dear ScriptingHelpers Community,

My script kind of works but I am not able to toggle my imablelabel visible state. Here is my code:

local popup = {} 
 
local windowGui = nil
local windowImage = nil

function popup.create()
	windowGui  = Instance.new("ScreenGui")
	windowGui.Parent = script.Parent

	windowImage = Instance.new("ImageLabel")
	windowImage.Parent = windowGui
	windowImage.Position = UDim2.new(0, 25, 0, 50)
	windowImage.Size = UDim2.new(0, 100, 0, 100)
	windowImage.Image = "rbxassetid://133293265"
end
 
function popup.toggle(state)
	windowImage.Visible = state
end
 
return popup

I can see my Image but if my script calls toggle it doesn't change the visible state. What is wrong?

Cheers, SmartLexx

1 answer

Maybe because it's nil? Module scripts act as dictionaries and the variables not connected to the module table will not be saved after you run a function.

local popup = {} 

popup.windowGui = nil
popup.windowImage = nil

function popup.create(self)
    local windowGui  = Instance.new("ScreenGui")
    windowGui.Parent = script.Parent
    self.windowGui=windowGui

    local windowImage = Instance.new("ImageLabel")
    windowImage.Parent = windowGui
    windowImage.Position = UDim2.new(0, 25, 0, 50)
    windowImage.Size = UDim2.new(0, 100, 0, 100)
    windowImage.Image = "rbxassetid://133293265"
    self.windowImage=windowImage
end

function popup.toggle(self,state)
    self.windowImage.Visible = state
end

return popup

Then you call it as a module instead of a function.

local popup=require(popup)
popup:create()
popup:toggle(true)

popup.create(popup) is the same as popup:create()

4 comments

0

Well, I call create() first and thats why I see an Image. After that I call toggle() and it do nothing. Not visible false and true ... it doesn‘t switch.

0

Well yeah. I'll edit my post with a working code.

0

Why do I need to use require()? Anyway I will test this code today. Thank you

0

Because that's how ModuleScripts are used. They are meant to serve as a table for Scripts and LocalScripts and can't run on their own.

This discussion is preserved as it was. New answers, comments, and votes are closed.