Using parameters as local variables

So a long while back I learned a trick with Lua coding where you could create variables in the parameters that you wouldn’t pass to, and use those instead of local variables.

During the time, there was a practical reason to do this in order to avoid memory leaks in a faulty game engine.

I’m curious about the practicality of using parameters for variables in the Roblox engine.

I.E.

function foo(bar)
    bar = Instance.new("World")
    print(bar)
end

What impact would this have on the code?

1 Like

Doing this would only save on so so so so little memory, that it doesn’t even matter, since after the function finishes up, it’ll just nil out anyway.

What this will do is set a Argument as a Instance.

It wont make a big impact, you can always change it to a specified value :slight_smile:

function foo(bar: Instance) -- Assigned as an Item

They are usually use for Sending Data

local function myFunction(x, y, z)
   return x+y+z
end

print(myFunction(1,2,3)) -- prints "6"

local Number 

function A(x)
Number = x
end


print(A("Hi"))

I would personally use this:

local function someFunction(bar)
    local bar= bar or Instance.new('World')
    -- whatever
end

but your method really doesn’t change anything, or the change is so slight that it’s considered the same thing.

Probably none, as far as I know, a local variable would have no more memory impact than an empty parameter initialized as the value in Luau. As Studio is a mostly online game engine (i.e. you have to be connected to the internet to start it up), even if this issue existed someone could make a GitHub issue on their official page and it would be fixed for everyone.