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

What's the point of putting nil in a variable assigning a variable to nil waste of line?

isn't assigning a variable to nil like kind of useless and a total waste of line?

noob = nil
poop = nil
sh = nil
roblox = nil
noob = nil

--and then assign all of them as we go


noob = 'LOL'
poop = 'ASASDASD'
sh = 'ASDASDADADASDASD'
roblox = '324u128944'
noob = '2481293123'

Is there any explanation for why people would do this?

2 comments

0

Remember to accept my answer if it helps!

0

It's for declaring locals without initializing them. Assigning a global to nil is indeed pointless, like you said. You can achieve the same result by stating local alpha instead of stating local alpha = nil

1 answer

✓ Accepted answer

#Actually, it isn't!


I know it may seem weird, but pre-defining variables keep our Code cleaner, and more organized. Variables don't have to be set immediately when they're defined. Maybe you'd like a Global variable with a piece of information that can only be accessible later on, well this can be achieved with this manner, instead of having to define it locally later on. - Which can be a pain, especially if desired to have accessble outside the scope.


Setting a variable to nil at a later time as well is beneficial too. Somtimes you'd like to resuse a variable with different information, or would like to erase the current information it's referencing for said reuse, or "trashing" of the variable. - Remember when you're a hundred percent done with something, don't leave it hanging around.


Though, this wouldn't necessarily be ideal to be formatted in such a way. It usually isn't too. There's a likely chance you've actually seen this before, but this is actually commonly written like so:

local Float
--// Later point
Float = 2.5

Another useful addition to this is being able to tie Gobal references to Events. No, not RemoteEvents, I'm talking about Signals and Listeners. This is useful as you can directly manipulate these RBXScriptSignals according to it's attached reference. This is commonly used to :Disconnect() Listeners`.

local RenderService = game:GetService("RenderService")

local Stepped

Stepped = RenderService.RenderStepped:Connect(function()
   print("Some random example")
end)

Stepped:Disconnect() --// Perrmanently unties the Listener from the Signal.

They'll just keep going! It's up to you to figure out how this use of pre-declaration fit's, and benefits you best!

2 comments

0

You never ever ever ever ever need global variables. If you do end up finding a need for them your codes need restructuring.

0

They wouldn't exist if nobody needed them.

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