Getting returns and calling a function at the same time?

So i have an odd question and I may just be being stupid however, I’m working on a shop GUI that will add items from a folder so I have variables like currentWidth, CurrentRow, etc and I have a function to add items and return the width and row n whatnot after it adds an item. however it takes in the current width to work, Is there a way that I can use the variables it returns and call the function to do its thing at the same time? I was thinking of doing

currentWidth, currentRow, lastItemPos = add(v, frame, lastItemPos, currentRow, currentWidth, maxRows, maxWidth, player)

But I thought it would only get the returns and not actually add the items and if I did the add() function afterwards it would work off of the increased values, am i just not thinking hard enough? Any help would be very much appreciated

1 Like

When you call a function with multiple return values, those values will be assigned to the variables on the left-hand side of the assignment. If you want to use the updated values in subsequent calls, you can do something like this:

currentWidth, currentRow, lastItemPos = add(v, frame, lastItemPos, currentRow, currentWidth, maxRows, maxWidth, player)
currentWidth, currentRow, lastItemPos = add(another_v, another_frame, lastItemPos, currentRow, currentWidth, maxRows, maxWidth, player)

This way, the updated values are used in the next call to add . Each call to add will use the values of currentWidth , currentRow , and lastItemPos from the previous call, incorporating any changes made by the function.

The function is supposed to change gui elements and then return the variables for the next call, I thought the function would just change the variables and not actually run as in changing the GUI elements?

I think this is what you are asking…
It sounds like your code is a bit like this abstract version. Test it out to see what will happen.


local function add(var)
    print("whatever code I want to run will run here")
    return var + 1
end

local Y = 1

for i = 1, 10 do
   print(i, "old value", Y)
    Y = add(Y)
    print(i, "new value", Y)
end

You can pass a value into the function but not the returned value, the new value doesn’t exist until the function has returned, but once it has returned you can.

Slight correction

local function add(var)
    print("whatever code I want to run will run here")
    return var += 1
end

Needs to be:

local function add(var)
	print("whatever code I want to run will run here")
	var += 1
	return var
end

Else you’ll get the error:
ServerScriptService.Script:3: Expected 'end' (to close 'function' at line 1), got '+='

1 Like

yes, you are right, thanks for noticing, it’s the assignment operator
return var + 1 should work though.
(Edited now)

1 Like

Im trying to update the values while running the function as the function changes GUI elements it has to do its changes and whatnot as well as updating the values at the same time so it can be run again

Do you mean like this?

local X = "A"
local Y = 1
local function add()
    --Change Gui elements here
    --then change values
    Y += 1
    X = X:rep(2)
    print(Y, X)
end

for i = 1, 10 do
   add()
end

Yeah, i should probably just change values instead of importing as arguments thanks for your help!