✓ 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
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()
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
Remember to accept my answer if it helps!
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 alphainstead of statinglocal alpha = nil