The Goal
So what I am trying to do is to make a minigame place. It checks if the minigame has a pointbrick and if it does then it means that the minigame is a race. We have to detect when that brick is touched so then when touched it will end the minigame.
The Problem I am not experienced with the 'return' code so can you help with this? The Code
01 | function round() |
02 | local minigames = game.Lightning:GetChildren() |
03 | local map = minigames [ number ] |
04 | local number = math.random( 1 , #minigames) |
05 | local spawns = map.Spawns.GetChildren() |
06 | local pointbrick = map.PointBrick |
07 | for intermission = 25 , 1 ,- 1 do |
08 | game.Workspace.Timer.Value = "Intermission: " ..intermission |
09 |
10 | end |
11 | game.Workspace.Timer.Value = "The game will start shortly." |
12 | wait( 3 ) |
13 | game.Workspace.Timer.Value = "Choosing Minigame..." |
14 | wait( 3 ) |
15 | game.Workspace.Timer.Value = "Minigame: " ..map.Name |
What exactly are you trying to return
, and why return
anyway? All the returns you have there are pointless. And even without return
this script doesn't work.
return
is used in functions which are used to process a value and return another. It is also used to cut a statement or loop short.
Here's an example of a function with return
:
01 | --Read game.Players.PlayerAdded first! |
02 |
03 | function getId(user) -- In the function we decide to refer to the Player instance as "user" |
04 | id = user.UserId --We make this variable that gets the player's UserId |
05 | return id --The variable we got above returns to the event function. |
06 | --This means that, all lines which are inside the event function and which are after getId below now know a new variable: id. |
07 | end |
08 |
09 | game.Players.PlayerAdded:connect( function (player) |
10 | getId(player) -- The Player instance that was added is taken for processing at the function |
11 | print (id .. " has joined the server" ) -- The userid that joined gets printed. |
12 | --Note that in some cases where you want an interger/number to be a string you must use tostring(number). |
13 | --For reasons i don't know the script works without it. |
14 | end ) |
Here's an example ofreturn
used as an interruption:
01 | --Back in my early days on Roblox this is just what follower NPCs were. |
02 | repeat |
03 | --Suppose we have script on here that finds closest player and sets a global variable used below |
04 | repeat |
05 | if player = = nil then return end -- The repeat loop ends abnormally if we have no player or his character |
06 | if player.Character = = nil then return end |
07 | script.Parent:MoveTo(player.Character.Torso.Position) |
08 | wait( 0.07 ) |
09 | until not (player and player.Character) --The repeat loop ends normally on this case |
10 | until false --This makes a repeat loop endless, provided there is no return for this one |
Here's your script fixed (which doesn't need return
as anything):
01 | function round() |
02 | local minigames = game.Lightning:GetChildren() |
03 | local map = minigames [ number ] |
04 | local number = math.random( 1 , #minigames) |
05 | local spawns = map.Spawns.GetChildren() |
06 | local pointbrick = map.PointBrick |
07 | for intermission = 25 , 1 ,- 1 do |
08 | game.Workspace.Timer.Value = "Intermission: " ..intermission |
09 |
10 | end |
11 | game.Workspace.Timer.Value = "The game will start shortly." |
12 | wait( 3 ) |
13 | game.Workspace.Timer.Value = "Choosing Minigame..." |
14 | wait( 3 ) |
15 | game.Workspace.Timer.Value = "Minigame: " ..map.Name |
I hope i helped with both the script and understanding return
.
So I saw the answer. But you do realize that the game is a minigame place and that it will actually go like this?
01 | function round() |
02 | local minigames = game.Lightning:GetChildren() |
03 | local map = minigames [ number ] |
04 | local number = math.random( 1 , #minigames) |
05 | local spawns = map.Spawns.GetChildren() |
06 | local pointbrick = map.PointBrick |
07 | for intermission = 25 , 1 ,- 1 do |
08 | game.Workspace.Timer.Value = "Intermission: " ..intermission |
09 |
10 | end |
11 | game.Workspace.Timer.Value = "The game will start shortly." |
12 | wait( 3 ) |
13 | game.Workspace.Timer.Value = "Choosing Minigame..." |
14 | wait( 3 ) |
15 | game.Workspace.Timer.Value = "Minigame: " ..map.Name |