How to access a top level variable that's being shadowed by local variable?

I’m running into a situation where I have conflicting variable names. The issue stems from the fact that I tend to use descriptive name which indicates what the variable is being used for. The problem is that I have a local variable defined in a function that shadows a top (script) level variable of the same name. What I would like to do is access the top level variable from the function. I have done this in C++ by doing this:

int someVar = 42

void someFunction(void)
{
	int someVar = 24
	::someVar = someVar
}

This uses the scope operator to access the shadowed variable in the global context instead of the local variable.

Can this be done in LUA? If so, how? I tried using the scope operator but it errors out. I don’t know what the syntax looks like.

local someVar = 42

local function someFunction()
	local someVar = 24
	::someVar = someVar	-- Error
end

The above code is what I tried.

local someVar = 42
local _someVar = 24

1 Like

To my knowledge Roblox Lua doesn’t have this feature, what I do is if they are conflicting then my local variables will have an underscore before them.

Looks like it’s time to put in a feature request.

@DataSigh, @r_apt Thanks.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.