How to use functions that are defined by a sandbox, but not the script

I’m looking to add functions using a sandbox,

I have so far looked through DevForum and DevHub for solutions and found nothing.

Basically trying to use a function that is not defined, will setfenv work for this? Or should I use something else.

I’m not quite sure what you’re trying to do here. I know what a sandbox is, but you aren’t giving much else other than “looking to add functions using a sandbox” add functions to what and for what?

So basically, kind of like this.

example()

The function “example” is not defined, though it would still run a function, except I’m not sure what to use to achieve this.

I was first thing that maybe setfenv would work to add a function. I am first making sure if I should use something else, or if setfenv would be good.

Or possibly getfenv

So basically the script environment is what I’m trying to add said functions to.

I really don’t know what you’re trying to do here.

You can’t do

function example()
  --Code
end

?

Are you trying to make these functions accessible from other scripts? If so, you can just put functions in a module script. If you’re trying to make global functions (functions defined in one script that can be used in any script in the game.) unless there’s a new way, Roblox patched this.

Have you heard of “EzConvert Portable”

I can’t say that I’ve heard of that.

I took this directly from the module,

--[[
	EZCONVERT
	CREATED BY NEBULA_ZOROARK [ROBLOX USERID 5719877]
	
	Gives a script a localscript-like environment.
	To use EzConvert, you must parent the script to their PlayerGUI or Character (preferably Character)
	Just call this require as a function (require(id)() or require(path)()) to automatically add a localscript-like environment.
--]]
	

Basically I’m trying to allow someone to use require with the modules ID on the Creator Marketplace / Library, e.g.

require(#)()

As stated in the excerpt from above,

To load in a library of new functions to use, basically using this as a way to make scripting something easier by pasting one line.

To be honest, EzConvert is irrelevant but I was checking to see if you knew what it was to give an easier idea.

(I am NOT trying to achieve a replica of this environment, just the way it does what it does with “adding functions”. I’m sure there’s another word for this but I cant quite put my finger on it)

So if I get what you’re saying, you’re trying to have a localscript / script, that can require a modulescript, then access functions from that modulescript in the localscript / script?

Ohhh no. I know how to do that, thats simple. I’m attempting to require a module as a function, and that said function would add new variables which can be called as functions,

For example lets say I want to add a function for printing something, pure example,

function eg(v)
print(v.." // Printed from function eg!")
end

Lets say I want to add that to the scripts current environment, what should I go to to figure this out, I’d rather learn it myself than be given a system for it, as one its against the usage of this tag, and is better to learn yourself, as I have learned from my past posts.

Like which articles to see for this.

Once again I am trying to do this all from a modulescript, and from what I’ve seen in the EzConvert module, I’m pretty sure that just involves setting

setfenv(2,environment)

Correct?

Instead of

setfenv(1,environment)

or

setfenv(0,environment)

So basically you trying to do something like this?

--ModuleScript

local Module = {}

function CustomPrint(...)
  print("CustomPrint:",...)
end

return function()
  --Stuff in here that would pass CustomPrint() over
end
--LocalScript

require(path.to.Modulescript)()

CustomPrint("Hello", "World")

?

Exactly what I’m trying to achieve!

I know that back in the day we had a cheat to make global variables accessible from any script using getfenv() and setfenv() but I know that way is long past being patched.

You might be able to just call getfenv() and pass that into the function you call in the ModuleScript, then use setfenv(CustomPrint, envWePassedIn)

Yes, that might work, but EzConvert (which I talked about above), still works, and does this somehow.

If you wish, I can send you the modules ID in private messages.

I’m talking about something completely different. We were able to make a script and just run it. Then without even requiring the script, we had access to the functions we set it there from any script, anywhere in the game.

Ah, okay. I did notice getfenv being called inside the function. So… I will check if that works.


This seems to work but the script editor is confused by it lol.

1 Like

Ah, thank you, except I did state I would rather learn it myself.