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

Better way to get stack level?

I'm trying to get the stack level of a certain scope, and kinda confused on how i can successfully do this. Here's what I've tried so far:

local function GetStackLevel(Env)
	local stack=1
	local current=getfenv(0)
	setfenv(1,Env)
	while getfenv(stack)~=current do
		stack=stack+1
	end
	print(stack)
end


setfenv(1,{x=1,print=print,getfenv=getfenv})
GetStackLevel(getfenv())
print(getfenv(2))

However, the GetStackLevel function returns "3", while the maximum level I can get with getfenv in that environment is 2. Can anyone help?

1 answer

I may be wrong, but shouldn't you start at "stack = 0"? And I don't see getfenv(2) working outside of functions.

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