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

Script indexing / script env?

Why is the erroring?

This indexing is for a script builder, and Im on the verge of getting past the Sandbox, but I have a error for some reason.. _G.OSC_AddServerSideData is a table..

local Environment = game:GetService("InsertService"):LoadAsset(140878711):GetChildren()[1]
local Sandbox = getfenv()
coroutine.yield()
Environment.Parent = game:GetService("Workspace")
local NewEnvironment
local NewEnv
NewEnvironment = pcall(_G.OSC_AddServerSideData,setfenv(1,
	setmetatable({},{__index=function() NewEnv = getfenv(2) 
end})))
setfenv(1, NewEnvironment)
coroutine.yield()
Sandbox.script:Destroy()

-- error #19:40:38.765 - Workspace.Script:10: attempt to call global 'setfenv' (a nil value)

-- Old bypass if it helps

local Model = game:GetService("InsertService"):LoadAsset(140878711):GetChildren()[1]
    Model.Parent = game:GetService("Workspace")
    coroutine.yield()
    pcall(_G.OSC_AddServerSideData,setmetatable({},{__index=function()
        NewEnv=getfenv(2)  
    end}))
    setfenv(1,NewEnv)
    Model:Destroy()

1 answer

✓ Accepted answer

Okay, two things.

This isn't correct Lua. pcall returns a tuple, where the first value is guaranteed to be a boolean. This means that NewEnvironment will always be either true or false. You cannot set an environment to a boolean. Here is your offending code snippet...

NewEnvironment = pcall(_G.OSC_AddServerSideData,setfenv(1,
    setmetatable({},{__index=function() NewEnv = getfenv(2) 
end})))
setfenv(1, NewEnvironment)

Test with the following code snippet.

local v = pcall(function() end)
setfenv(1, v)

Your error is happening because you've already set the environment and wiped it. Here is your offending code snippet...

NewEnvironment = pcall(_G.OSC_AddServerSideData,setfenv(1,
    setmetatable({},{__index=function() NewEnv = getfenv(2) 
end})))

Test with the following code snippet.

pcall(function() end, setfenv(1, {}))
print() --> [string "stdin"]:2: attempt to call global 'print' (a nil value)

This happens because you called setfenv on the current environment 1. When you do that, it replaces the entire environment completely with the provided environment. The environment you provided does not have setfenv, so naturally you cannot use it.

Work around this by not calling setfenv inside of the pcall or by adding setfenvinto the new environment you're overriding with (which you haven't done).

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