How to return multiple values?

Well, I am trying to return two values ​​but doing so gives me the following error: 253: attempt to index nil with number. Before I put the two values ​​to return, it was not giving me this error.

This is how I return the values:

Get_objects.OnClientInvoke = function(Service)
	if Service == "Get_items" then
		if Selected.Value ~= nil then
			return {Selected.Value, Option_Seletected_bool.Value}
		end
	end
end
3 Likes

Can you please show me the full code

Are you put this script in module script?

function foo()
     return 1, 2
end

local foo1, foo2 = foo() -- foo1 == 1, foo2 ==2
32 Likes

No, it is a normal script(Localscript and script). The script that returns the data is a bit long, also the one that resumes it

Can you send a small screen shot or smthing of the part where it tries to use the data from the returned table?

Wait I know what you did, remove the curly brackets, you don’t need those

You do need them I think. You can only send one value through at a time if u do that. What he has tried to do is send a table through with mutiple values in which I think is the right way could just be the way he handels it on the other end.

ok then do this:
local tabletoreturn = { a = Selected.Value, b = Option_Seletected_bool.Value}

return tabletoreturn

1 Like

What he did is exactly that just without the variable which should not affect it. Your still sending the same thing.

One other way he could try is by sending the data via an array not sure.

On the other end he should have some type of variable to collect the return which then can be used like a table like if the variable on the other end was called Response then it would be Response[0] to get the first thing from the table.

1 Like


It is an advanced cooking system

Just put the table in a variable and return the table instead of creating a table whilst returning, that could be causing the error.

Is the Slots what ur returning? Like as the table I mean

I am asking cuz on that screenshot I see nowhere a [0] on it which would be the first value returned Could the problem be that u thought the first value was [1] so the [2] is then nothing (nil)?

Slots are multiple tables within a table: kitchen utensil, ingredient, complete, slot_moment and clickDetector

What the the table which is returned then?

local Get_Data = Get_objects_InvokeClient(player, "Get_items")

print(Get_Data[1]) --tool
print(Get_Data[2]) --BoolValue

return:

Get_objects.OnClientInvoke = function(Service)
	if Service == "Get_items" and Selected.Value ~= nil then
		return {Selected.Value, Option_Seletected_bool.Value}
	end
end

What does the print(Get_Data[2]) because I think the second value should be [1] not [2] cuz if I remember correctly tables start at [0] and u only returned 2 values.

(might be wrong on that cuz I have not used tables for a while only in JS cuz i have been coding in JS more recently)

Okay in that part, start from 1 and not from 0

I think that may be the issue. Try changing all of the Get_Data which is [1] to [0] and and then the [2] to [1]

If what I am thinking then it would make sense cuz the [2] value would be nil as stated in the error

Apparently I already fixed it, if it was like in this example it seems: