Is It Possible To Have A Function Return Two Variables A Value?

Hello! I have been trying out a few simple code things, and I found possibly the best way for my variable to be called is from inside a function. Here is my code:

local X = Instance.new("StringValue", workspace) -- Create The String Value
local Y = true -- Set Y To True As An Example
X.Value, X.Name = function() if Y then return true, "Test" else return false, "Bad Test :((((" end end -- Function To Set The Value Of X And The Name?

I was curious if this would work or if I should do something else to get this kind of result?

Solution:

function Z(Y) if Y then return "true", "Test" else return "false", "Bad Test :((((" end end local X = Instance.new("StringValue", workspace) -- Create The String Value
local Y = true -- Set Y To True As An Example
X.Value, X.Name = Z(Y) -- Function To Set The Value Of X And The Name?
2 Likes

But what about setting two variables to the same value. Like X, Y = {3, 2}?

I thought i did :confused:. Thats weird.

local X = Instance.new("StringValue", workspace) -- Create The String Value
local Y = true -- Set Y To True As An Example
X.Value, X.Name = function() if Y then return true, "Test" else return false "Bad Test :((((" end end -- Function To Set The Value Of X And The Name?

It works you forgot the comma in the else statement’s return behind false

You can look into pcalls, the syntax for this isn’t special

Why? They’re not relevant here.

2 Likes

that didn’t work:

local X = Instance.new("StringValue", workspace) -- Create The String Value
local Y = true -- Set Y To True As An Example
X.Value, X.Name = function() if Y then return true, "Test" else return false, "Bad Test :((((" end end -- Function To Set The Value Of X And The Name?

You’re trying to set them to a function without calling the function. Essentially you’re just setting x.Value to a new function (which doesn’t work). Either call the function or use ternary* instead, like so:

X.Value, X.Name = Y, Y and "Test" or "Bad test :(((("
1 Like

I think I found the solution:

https://education.roblox.com/en-us/resources/returning-values-from-functions--code-fundamentals

You were returning the values from the function correctly, the function was just never called. Not calling a function means it won’t return anything, as you’re just referencing the function and not using it.

2 Likes

Also this doesn’t and won’t work since my actual code is using Strings and Color3s

You can just change the code to suit your needs, like any other code. boolean and firstResult or fallback should work in a more general sense than this one situation, as long as firstResult isn’t false or nil.

1 Like
function Z(Y) if Y then return "true", "Test" else return "false", "Bad Test :((((" end end local X = Instance.new("StringValue", workspace) -- Create The String Value
local Y = true -- Set Y To True As An Example
X.Value, X.Name = Z(Y) -- Function To Set The Value Of X And The Name?
1 Like

this worked. Thank you so much :smiley:

1 Like