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

Module script functions throws "attempt to call a nil value" error?

So I have a module script with a lot of common variables and functions and I also have a local script which uses those functions, but when I call a function from the module script, it throws up an error.

Module Script:

local CommonResources = {}
CommonResources.Frame = script.Parent.Parent.Frame
CommonResources.Main = script.Parent.Parent.Frame.Main
CommonResources.Welcome = script.Parent.Parent.Frame.Welcome

CommonResources.Battery = script.Parent.Parent.Data.PerSession.Battery
CommonResources.SessionStarted = script.Parent.Parent.Data.PerSession.SessionStarted

function HideFrame(Frame)
	Frame.Visible = false
	Frame.Active = false
end
function ShowFrame(Frame)
	Frame.Visible = true
	Frame.Active = true
end
	
return CommonResources

Local Script:

local CommonResources = require(script.Parent.CommonResources)
local Main = CommonResources.Main
local Welcome = CommonResources.Welcome
local Frame = CommonResources.Frame
local Battery = CommonResources.Battery
local SessionStarted = CommonResources.SessionStarted

CommonResources.HideFrame(Main)
CommonResources.ShowFrame(Welcome)
CommonResources.ShowFrame(Frame)

CommonResources.SetZIndex(Frame, 1)
CommonResources.SetZIndex(Main, 2)
CommonResources.SetZIndex(Welcome, 3)

Battery.Value = 100
SessionStarted.Value = false

Error: Scripts.SetDefaults:8: attempt to call a nil value

2 answers

If you're calling a function that is in a module script you need to do:

CommonResources:Function() --notice colon not period, or that's what I do anyway and it works fine

if its still not working try

require(script.Parent:WaitForChild(CommonResources",2)

I fixed it.

I had to turn

function HideFrame(Frame)

Into

function CommonResources.HideFrame(Frame)

In the Module Script

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