What are scopes and what do they do?

Hello, I don’t understand how scopes work. I’ve googled and watched vids but I wasn’t able to understand any single one of them but I wa able to learn that they could be used in functions. Can any of you guys explain this in a simplified version.

Roblox has articles on fundamental subjects like this:
https://create.roblox.com/docs/luau/scope

couldn’t understand anything but I do understand that they are used in functions

Scopes are basically functions/variables that can only be used in certain context, there’s global and local context. For example here:

local str = "Hello"

print(str) -- prints "Hello"

Here the str variable is on the global scope, which means any function can use it. And is not local, but if we put the string in a function:

function RandomFunction()
	local str = "Hello"
end

print(str) -- prints nil

The str variable is now confined to that function and cannot be used outside. The str variable is on a local scope now.

3 Likes

Thanks for the simplified explanation. I was able to understand every single bit, thanks for trying to help too Ziffix


The article covers this exact example. Don’t give up so early. There is much more to know

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