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

Calling Module function from a Local script?

Hey guys, having an issue trying to call a function from a Module script in a local script.

Module Script:

function Settings.Update_Gui(Ammo, StoredAmmo)
	if Settings.Gui and Settings.CanUpdateGui then
		Settings.Gui.Frame.AmmoDisplay.Text = Ammo .. "|" .. StoredAmmo --Line 77
	end
end

Local Script:

local Ammo = script.Ammo -- NumberValue
local StoredAmmo = script.StoredAmmo -- NumberValue

Settings.Update_Gui(Ammo.Value, StoredAmmo.Value)

Error: Players.Player1.Backpack.M1911.Settings:77: attempt to concatenate local 'StoredAmmo' (a nil value)

Let me know if you can figure out why I am getting this odd error or if you need a bit more information.

Thanks.

1 comment

1

For whatever reason, StoredAmmo is not being passed to the Update_Gui function. Use some print() statements on both sides of the function call (inside and outside the Module script) to see at what point you're losing the data.

2 answers

I was having this problem myself. It seems that the passed-in arguments get disordered for some reason. I resolved it by passing them in as a tuple instead and then parsing it. In your case I would do the following:

function Settings.Update_Gui(...)
	local tuple = {...}
	local Ammo = tuple[1]
	local StoredAmmo = tuple[2]
	if Ammo and StoredAmmo then
	    if Settings.Gui and Settings.CanUpdateGui then
	        Settings.Gui.Frame.AmmoDisplay.Text = Ammo .. "|" .. StoredAmmo
	    end
	end
end

And you don't have to change your localscript call.

Well, your first error is that ModuleScripts cannot be called by LocalScripts. Use a ServerScript to use a ModuleScript.

Your second error is that you're not waiting for the children of the script to exist, so:

local StoredAmmo = script:WaitForChild("StoredAmmo")

8 comments

0

Hmm, Module Script cannot intereact with local scripts? Interesting. Can you provide a link to that? It doesnt mention that on the module script roblox page.

0

Also, as a side note. The second bit of information is not the problem. Thanks for the idea though.

0

ModuleScripts can totally be used normally by localscripts. There is no limitation there.

0

Thought so. Thanks @BlueTaslem

0

? I read on wiki... can you please not downvote? I got it wrong cause I read it on the wiki...

0

Where did you read this 'fact' on the wiki? It's 100% wrong, and I'll be glad to change it if you can link me to it.

0

Also, you can remove your answer if it gets negative attention or is incorrect. Just for future reference

0

OK, will do. Sorry about this :P I believe I read a sentence in the description of how to use them, interpreting it as 'cannot be used by localscripts'

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