What do you want to achieve? I want to know what returning is in scripting.
What is the issue? I watch tutorials and they never seem to hel
What solutions have you tried so far? Youtube, Developer Hub
Here is a script i recently seen that had me even more curious
local chosenPiggy = players[RandomObj:NextInterger(1,#players)]
return chosenPiggy
Now please help. I cant every seem to understand what it means or does, and how did you learn it?
Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.
In simple terms, returning is basically something that you retrieve within 1 function
In the code you have, if you return chosenPiggy, you can retrieve its results if you manage to call it from outside the function
local function ChooseRandomOinky()
local chosenPiggy = players[RandomObj:NextInterger(1,#players)]
return chosenPiggy --This would be either nil, or the Random Player you want to choose from
end
local ConfirmPlayer = ChooseRandomOinky() --This is how we obtain a returned value, by calling the function
if ConfirmPlayer then --We're checking if this isn't equal to nil
--Now do your stuff here
end
So what I’m getting from this is when I return something and call the function somewhere else its returning whatever i returned making me able to use the same line again right?
function random()
local part = game.workspace.Part
return part
end
local colors = part
if colors then
colors.BrickColor = color3.fromRGB(“255,255,255”)
Close, you would reference the function that returns the object.
function random()
local part = game.workspace.Part
return part
end
local colors = random()
if colors then
colors.BrickColor = color3.fromRGB(255,255,255)
end
@TinyFlair No, because part would be equal to nil in this instance cause it’s defined as a local variable inside the function, and you’re not calling the random() function that the Part is returning back
Let’s look at this a visual way.
We’ll use print as an example here.
function getSentence(name) --Grab our input (the name we want to display)
return "Hello " .. name .. "! How's it hanging?" --Return a new string with our text and name involved
end
print(getSentence("Jimmy")) --Expected output: "Hello Jimmy! How's it hanging?"
print(getSentence("TinyFlair")) --Expected output: "Hello TinyFlair! How's it hanging?"
You would just be simply ending the function early
Now that you’ve got a basic & general concept of how returning works, let’s expand it a bit more
Say I have this function
local Bool = false
local function CheckBool()
if Bool == nil then
return --This is basically returning nothing
end
if Bool == true then
return "This Bool is true!"
elseif Bool == false then
return "This Bool is false!"
end
print("This will never print because we're actually returning/ending our function at least once")
end
print(CheckBool())
Now, we got a little bit more things here, but let’s go ahead and break this down here
First, we’ll start with the nothing return
This would check if the Bool variable hasn’t been assigned to anything, and if it is then we can end our function early to prevent it from continuing the script! Usually you want to return if you don’t want to continue the function
Next, is our Bool check returns
Simple stuff, but this will check if our Bool variable is true/false, if it’s 1 of the 2 then we can return back a string confirming what Value our Bool is!
Lastly, we print the result of the returned value and it will either return 3 things:
nil - If the Bool variable hasn’t been assigned/defined by anything
false - If the Bool variable is equal to false (Which would be the False String)
true - If the Bool variable is equal to true (Which would be the True String)
When you do
local someinstance = Instance.new(instance)
This function returns you the instance and that is the reason you are able to store it in the variable.
So imagine this function
> local function Add(a,b)
>` return a + b`
> end
> local result = Add(3,3)
> print(result) --> 6
This function is returning you the sum of the arguments provided, which you can then store into a variable.