Is it possible to do Variadic variables?

Heya :camel:
I’m working on this getting info server invoked function, when I came a problem or more like a question about variables in general.

As a declaimer I’m not sure if this is good practice or not if it I should just make it with a table instead of multiple modules inside the script that just return functions. Correct me if I’m wrong or it is a bad practise and what I should utlize instead.

function ServerInvoke(Player, FunctionName, ...)
	if not Player then return end
	if not script:FindFirstChild(FunctionName) then Player:Kick("incorrect function name call") return end
	
	local Promise = require(script:FindFirstChild(FunctionName))(Player,...)
	local worked,value = Promise:awaitStatus()
	if worked then 
		return value 
	else
		
	end
end

So, the awaitStatus function will return worked and possibly more than one value but I index it as if it were just one value it can possibly return after the worked variable.
I’m curious if there’s a way to return everything after the worked variable for example like:
local worked, (...) = Promise:awaitStatus()
I’m not sure if that’s possible or what I can do to get it to function correctly.

Thank you for reading this and have a good day!

Yeah, it’s totally possible. I’m assuming since Promise contains the awaitStatus() method, that it was created with OOP in mind. That function, just like any other lua function is able to return values at the end of it. So in the awaitStatus() method, if you worked and value to return, you would in the awaitStatus() method you would have something like:

function Promise:awaitStatus()
return true, 50
end

I’m assuming what is in the contents of the function, but as you can see I can return those 2 values back into the original script by using the return statement

And it doesn’t work just for 1 or 2, you can add an infinite amount of returns theoretically (probably not the best practice).

As for having multiple modules and if it’s good practice or not; the short answer is it depends. You don’t want your code all clustered up into one script, nor do you want every single part separated. But I’m sure you know Require takes up some processing power, so it’s best if you use these items intermediately.

It seems you have require and multiple modules for every single player, which is generally not good. You should think about making it so you can place the require outside the ServerInvoke function (the script only calls it once) and make it compatible with every player’s function call.

1 Like

The simplest solution is to have all the functions return an array of return values, and then do

if worked then 
		return unpack(returnValues)
end

to send them back as individual values to the client. You could also have a function like this:

local function onPromiseDone(success, ...)
	return success and ... or nil
end

and call it like this:

local Promise = require(script:FindFirstChild(FunctionName))(Player,...)
return onPromiseDone(Promise:awaitStatus())
2 Likes

Thank you so much for the super informative response, I love how you cleared my head on the topic of modules.
The modules were per function either way I did listen and its now just one module with a table that scripts place their functions in so the main script can call it at any time.

Now I have another question relating to this.
Is it bad practice to have modules in the UIs so others can call it out or should I use binable events instead. Or maybe should I try the method of just having one UI module and inserting the functions per screenUI inside?

image
this is what I’m currently doing, It’s easy to use but I have lots of worries of the performance side of things.