Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Can I use return to call a set of numbers?

Is it possible to use return to call a set of numbers? I'm sorry if this doesn't make much sense, I'm still kind of new to this and I'm not really sure how to word it right. Here is a section of my script so you can get a better idea of what I'm asking.

	if game:GetService("MarketplaceService"):PlayerOwnsAsset(plr,passId) then
		Val = DataStore:GetAsync("Pos")
		if Pos then
			return Pos
		else
			return -151.4, 36.5, -138
		end		


		plr.CharacterAdded:connect(function(Character)
			Hum = Character:FindFirstChild("HumanoidRootPart")
			HumPos = Hum.Position
			local Humanoid = Character:FindFirstChild("Humanoid") -- find the humanoid, and detect when it dies
			if Humanoid then
				Humanoid.Died:connect(function()
					wait(respawnTime) -- delay, then respawn the character
					plr:LoadCharacter()
				end)
			end
		end)
		
		plr:LoadCharacter()
		local HumPos = Val

On line 6 I am returning a set of numbers, is this possible or do I need to use a different approach? Thanks!

2 answers

✓ Accepted answer

Returning the numbers as a table would be one approach. Change line 6 to:

return {-151.4, 36.5, -138}

Then you can call each number with its corresponding table index, such as:

local myTable =  {-151.4, 36.5, -138}
local number1 = myTable[1]
local number2 = myTable[2]
local number3 = myTable[3]

1 comment

0

This is new to me, thanks for the help! :D

JetCrusherTorpedo is correct, usually you would want to return a list (table) of values.


In this case, it would be better to return a Vector3 since this encapsulate 3 numbers into one value, and is actually the way we represent positions:

return Vector3.new(-151.4, 36.5, -138);

However, you can do exactly what you did. You can return multiple values from a function. Those will be treated as consecutive arguments to a function it is applied to, or as several variables in an assignment:

function numbers()
	return 1, 2, 3;
end

local A,B,C = numbers();
local vec = Vector3.new( numbers() );

print(A); -- 1
print(B); -- 2
print(C); -- 3
print(vec); -- 1, 2, 3

3 comments

0

This too is really helpful, thanks! :D

0

One question, what are the semicolons for? What do they do?

0

https://scriptinghelpers.org/questions/6044/what-is-used-for It's (usually) just a stylistic choice. I always use them. Almost all of the time you never need to use them.

This discussion is preserved as it was. New answers, comments, and votes are closed.