Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How does script = nil work?

Asked by 8 years ago

I saw in one of 'Games' games, the ROBLOX account named games. The script is supposed to not be shown, another thing to ask is how would I see that it's not there.

script = nil

If something needs to catch your eyes, here is the main focus.

0
What. koolkid8099 705 — 8y

2 answers

Log in to vote
1
Answered by 8 years ago

script is a global variable that's already set in your script environment (as you probably know). Setting the variable of script to nil, will simply make it so you cannot reference your script anymore, which is probably not what you want to do. Going about this method would result in you not being able to use the "script" variable anymore in your code, unless you've assigned it some other value. For example:

script = nil

script.Name = "test"

This code would result in an error similar to this in the output:

attempt to index global 'script' (a nil value)

Parent property

What you're probably asking is how does the statement script.Parent = nil work. Which, is actually the same thing as using the Remove or remove method on an instance.

Parenting the script to nil, basically makes it so you can still reference the script, however anything else in the game cannot (essentially, removing the script from the game. As the method above I mentioned implies).

Summary

Anyway, needless lecture cut short, it removes the script from the game. If you have any further questions, just let me know.

Ad
Log in to vote
0
Answered by 8 years ago

Additional insight on environment tables
Because sometimes a big lecture can't cover everything

script in itself is a member of the function environment. This means that it exists in a table which is referenced every time there's not a local value with the same name, which is convenient for Roblox because it's also where Roblox keeps all of the extra things like game and workspace.

What this means is that by setting script to nil, it is being removed from the function environment. This is really important to people who want to keep modules secret, because it means that people can't grab script using getfenv, which would allow them to save the script source and take a look at it.
It is also useful for people making and using script builders, as it's important that the script sandbox doesn't ever let the script access the Script object that it is running from. Of course, this is a primitive sandboxing method, but it's always a good start.

In this case, by removing the script from any Parent and setting the script variable to nil, the script has become inaccessible to any external sources through any conventional means - Anything the script wants to do is likely to be exposed through _G or Bindables in a lot of cases.

Answer this question