Is "local" in variable definitions outside functions and blocks necessary?

Whenever I write scripts, I often think that code files that take up less bytes would run faster. As such, instead of writing this:

local numValue = 1
local boolValue = true
local ItemInWorkspace = Instance.new("BasePart",workspace)
local nilValue
local CONSTANT_VALUE = 10
local function someFunction(parameter)
	local valueInFunction = 1
	if type(parameter) == "integer" then
		return parameter + valueInFunction
	end
end

I would write this:

numValue = 1
boolValue = true
ItemInWorkspace = Instance.new("BasePart",workspace)
nilValue = nil	-- Just writing "nilValue" with neither "local " nor " = nil" is not accepted.
CONSTANT_VALUE = 10
function someFunction(parameter)
	local valueInFunction = 1	-- This "local" is retained for obvious reasons.
	if type(parameter) == "integer" then
		return parameter + valueInFunction
	end
end

Is it a good practice that you do not have to write local outside functions and blocks? If it is not a good practice, please explain.

Personnaly, i always use « local » as it helps me to read my whole script. So yeah when your script has 3 lines it isnt necessary. But once you have more than 50 or 100 lines, it would be usefull to be able to read it in one look without reading everything

1 Like

For some things it is necessary, like for Luau Type Checking, mainly it is used when a value must be within the “context of the function” (inside the block of the function).

Personally I use it for “non-physical variables” (required modules, numbers, etc) and for Type Checking.

1 Like

Always use local unless you can’t in the specific scenario, which is unlikely.

It’s slightly more performant as well, which is a bonus.

1 Like

Good advice; that would be a good reason to write local on every variable, even outside functions and blocks.