dDo something with a value thats isn't in the function

THis is what i meant to say if it wasn’t clear:

local test = true
local function testFunction()
	print(value)
end
if test == true then
	local value = 10
	testFunction()
end

I want to function to print the value that’s isn’t in it put in the if loop, is there an efficient way to do it.

local test = true
local function testFunction()
	print(value)
end
if test == true then
	value = 10
	testFunction()
end

print(value)

Just remove the local and then you can mess around with the value.

Well this code snippet will error, because value isn’t defined anywhere.
Is this what you mean?

local test = true
local function testFunction(value)
	print(tostring(value))
end
if test == true then
	testFunction(10) -- Change 10 to any number, and the function will print it!
    wait(1)
    testFunction(20) -- One second after 10 was printed, it will print 20!
end

You could go about this multiple ways:
@TheShowCaseMaker 's way which is probably what you mean, or:

local test = true
local value = 10
local function testFunction()
	print(value)
end
if test == true then
	testFunction()
end

The code snippet does not error. the value that you are passing into testFunction becomes a variable that your function can access. Also, when printing numbers with print you do not need to use tostring

OP’s question has to do with variable scope. That is where variables are able to be accessed. the simplest way to do this is to define it before as @Chromatype has shown. There is another way that you can do this though. You can do as @TheShowCaseMaker has done and create a global variable which is just a variable that does not have a local before it. This variable is able to be accessed anywhere. the local keyword before a variable makes the variable only be able to be accessed from its scope. You can read more about variable scope here: Scope | Roblox Creator Documentation

1 Like

Simply pass value as a parameter:

local test = true
local function testFunction(value)
	print(value)
end
if test then -- == true is not required
	local value = 10
	testFunction(value)
end

might be a burn out because i completely forgot parameter was a thing, sorry.

No need to be sorry. Glad everything was figured out.

1 Like