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

BindableEvent's passed argument isn't recognized when paired with ModuleScript?

--Note: This isn't my exact code, but represents exactly what I'm doing.

EventName.Event:connect(function(plr, cost, key)
local Module = require(plr.ModuleScript)
print(key) --Returns the key which is declared as: local key = "KeyName" in script firing the event.
print(Module.key) --Returns nil even though the exact variable inside the module script is already declared as: ModuleScript.KeyName = false, instead doesn't recognize it and sets it as nil.
if Module.key == false then --Doesn't find the key in the script even though it is already declared before this, so it will return: print("key is true or nil, hopefully not nil, should be true."), because it is nil as it didn't find it.
--do code like: key = true
else
return print("key is true or nil, hopefully not nil, should be true.")
end
end)


This really irritates me because this would make my code so much more simplisitic instead I'd have to write a lot code extra to cover up for it, is there a way to make it so it knows what key is and not assume it's nothing?

1 comment

0

No shortcuts.

1 answer

I'm not entirely sure what you're asking but from what I get you want it to basically understand the variable right away from the module? If so something I use when working with modules to make life a heck of a lot easier is this

put this at the end of your module for the return you do not need anything else in the module just treat it as a normal script that you're storing variables functions etc whatever in.

return setmetatable({},{
__call=function(t,env)
	for i, v in pairs(getfenv()) do
		if i ~= 'script' then
			env[i]=v
		end
	end
end
})

Now for the script you're hooking up the module to use this as the require.

require(script.Module)(getfenv())

This will essentially make it so your module is read as though it is just code already within the script your connecting it to. You do not need to make separate variables for functions from the module or whatever you get the idea. Idk if this helped with your problem but hopefully it did in some way.

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