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
function round() local minigames = game.Lightning:GetChildren() local map = minigames[number] local number = math.random(1, #minigames) local spawns = map.Spawns.GetChildren() local pointbrick = map.PointBrick for intermission = 25,1,-1 do game.Workspace.Timer.Value = "Intermission: "..intermission end game.Workspace.Timer.Value = "The game will start shortly." wait(3) game.Workspace.Timer.Value = "Choosing Minigame..." wait(3) game.Workspace.Timer.Value = "Minigame: "..map.Name wait(3) game.Workspace.Timer.Value = "Loading Minigame..." wait(3) mapModel = map:clone() mapModel.Parent = Workspace game.Workspace.Timer.Value = "Teleporting players..." wait(1) for _, player in pairs(game.Players:GetPlayers()) do local character = player.Character local randomSpawn = spawns[math.random(#spawns)] -- EDIT if character then character:MoveTo(randomSpawn.Position) -- EDIT end end wait(1) for readySetGo = 3,1,-1 game.Workspace.Timer.Value = readySetGo wait(1) end game.Workspace.Timer.Value = "Go!" for timer = 150,1,-1 do if pointbrick then function endRoundAuto() game.Workspace.Timer.Value = "Game over!" mapModel:remove() wait(3) return round end end game.Workspace.Timer.Value = "Time left: "..timer end if pointbrick then game.Workspace.Timer.Value = "Nobody won. Too bad." mapModel:remove() wait(3) return true else game.Workspace.Timer.Value = "Game over! Winners get __ points!" mapModel:remove() wait(3) return true end end if pointbrick then pointbrick.Touched:connect(endRoundAuto) end
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
:
--Read game.Players.PlayerAdded first! function getId(user) -- In the function we decide to refer to the Player instance as "user" id = user.UserId --We make this variable that gets the player's UserId return id --The variable we got above returns to the event function. --This means that, all lines which are inside the event function and which are after getId below now know a new variable: id. end game.Players.PlayerAdded:connect(function(player) getId(player) -- The Player instance that was added is taken for processing at the function print(id .. " has joined the server") -- The userid that joined gets printed. --Note that in some cases where you want an interger/number to be a string you must use tostring(number). --For reasons i don't know the script works without it. end)
Here's an example ofreturn
used as an interruption:
--Back in my early days on Roblox this is just what follower NPCs were. repeat --Suppose we have script on here that finds closest player and sets a global variable used below repeat if player == nil then return end -- The repeat loop ends abnormally if we have no player or his character if player.Character == nil then return end script.Parent:MoveTo(player.Character.Torso.Position) wait(0.07) until not (player and player.Character) --The repeat loop ends normally on this case 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):
function round() local minigames = game.Lightning:GetChildren() local map = minigames[number] local number = math.random(1, #minigames) local spawns = map.Spawns.GetChildren() local pointbrick = map.PointBrick for intermission = 25,1,-1 do game.Workspace.Timer.Value = "Intermission: "..intermission end game.Workspace.Timer.Value = "The game will start shortly." wait(3) game.Workspace.Timer.Value = "Choosing Minigame..." wait(3) game.Workspace.Timer.Value = "Minigame: "..map.Name wait(3) game.Workspace.Timer.Value = "Loading Minigame..." wait(3) mapModel = map:clone() mapModel.Parent = Workspace game.Workspace.Timer.Value = "Teleporting players..." wait(1) for _, player in pairs(game.Players:GetPlayers()) do local character = player.Character local randomSpawn = spawns[math.random(#spawns)] -- EDIT if character then character:MoveTo(randomSpawn.Position) -- EDIT end end wait(1) for readySetGo = 3,1,-1 game.Workspace.Timer.Value = readySetGo wait(1) end game.Workspace.Timer.Value = "Go!" for timer = 150,1,-1 do if not pointbrick then --Someone took the brick? End! --Make sure the pointbrick itself makes sure to remove when a player touches it. game.Workspace.Timer.Value = "Game over!" mapModel:Destroy() --Remove() sets parent to nil and the mapModel can be referred to again. Destroy() will remove it completely, as you will never want to refer to it again.. Helps for later performance. wait(3) timer = 0 -- Make this the last loop, it's all over else game.Workspace.Timer.Value = "Time left: "..timer -- Not done yet? Then keep counting, i guess. wait(1) --Wait before the next second counting end end if pointbrick then game.Workspace.Timer.Value = "Nobody won. Too bad." mapModel:Destroy() wait(3) else game.Workspace.Timer.Value = "Game over! Winners get __ points!" mapModel:Destroy() wait(3) end end --The script you had here was removed on purpose. The main script cannot multitask like that so you'll have to give the pointbrick something to do.
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?
function round() local minigames = game.Lightning:GetChildren() local map = minigames[number] local number = math.random(1, #minigames) local spawns = map.Spawns.GetChildren() local pointbrick = map.PointBrick for intermission = 25,1,-1 do game.Workspace.Timer.Value = "Intermission: "..intermission end game.Workspace.Timer.Value = "The game will start shortly." wait(3) game.Workspace.Timer.Value = "Choosing Minigame..." wait(3) game.Workspace.Timer.Value = "Minigame: "..map.Name wait(3) game.Workspace.Timer.Value = "Loading Minigame..." wait(3) mapModel = map:clone() mapModel.Parent = Workspace game.Workspace.Timer.Value = "Teleporting players..." wait(1) for _, player in pairs(game.Players:GetPlayers()) do local character = player.Character local randomSpawn = spawns[math.random(#spawns)] -- EDIT if character then character:MoveTo(randomSpawn.Position) -- EDIT end end wait(1) for readySetGo = 3,1,-1 game.Workspace.Timer.Value = readySetGo wait(1) end game.Workspace.Timer.Value = "Go!" for timer = 150,1,-1 do if not pointbrick then --Someone took the brick? End! --Make sure the pointbrick itself makes sure to remove when a player touches it. game.Workspace.Timer.Value = "Game over!" mapModel:Destroy() --Remove() sets parent to nil and the mapModel can be referred to again. Destroy() will remove it completely, as you will never want to refer to it again.. Helps for later performance. wait(3) timer = 0 -- Make this the last loop, it's all over else game.Workspace.Timer.Value = "Time left: "..timer -- Not done yet? Then keep counting, i guess. wait(1) --Wait before the next second counting end end if pointbrick then game.Workspace.Timer.Value = "Nobody won. Too bad." mapModel:Destroy() wait(3) else game.Workspace.Timer.Value = "Game over! Winners get __ points!" mapModel:Destroy() wait(3) end end while true do wait(1) round() end