I'm making a prop hunt game, and I want it so when the prop value reaches 0, the while true do loop stops and goes from the top again and starts a new round.
I'm getting really confused with these terms like "Return" and "Break"
; I know what they are, I just don't know how or where to use them so some help would be really helpful.
Here is the code - I want the script to break out of the playgame() function and start the next one in the while loop.
HIGHLIGHTED AREAS:
Function: Line 21 - Line 38
While Loop: 109-117
local lobbylocation = workspace.Lobby.Lobby.Position + Vector3.new(0, 3, 0) local gamelocation = workspace.Maps.Main.Position + Vector3.new(0, 3, 0) local replicatedstorage = game:GetService("ReplicatedStorage") local timeevent = replicatedstorage:WaitForChild("TimeEvent") local propcount = replicatedstorage.PropCount wait(3) print("everything loaded") game.Players.PlayerAdded:Connect(function(plr) plr.Team = game.Teams.Lobby replicatedstorage.ServerCount.Value += 1 end) game.Players.PlayerRemoving:Connect(function() replicatedstorage.ServerCount.Value -= 1 end) wait(1) local function playgame()--300 local timeAmount = 20 local timerText = "Time Left: " while timeAmount > 0 and replicatedstorage.PropCount ~= 0 do timeevent:FireAllClients(timeAmount, timerText) wait(1) timeAmount -= 1 if replicatedstorage.PropCount == 0 then print("HUNTERS WIN") break end if timeAmount == 0 then print("PROPS WIN") break end end end local function playintermission() local intermission = 10--60 local timerText = "Intermission: " while intermission > 0 do timeevent:FireAllClients(intermission, timerText) wait(1) intermission -= 1 end end local function resetplayers() for _, plr in pairs (game.Players:GetChildren()) do plr.Character.HumanoidRootPart.CFrame = CFrame.new(lobbylocation) end end local function teleportplayers() for _, plr in pairs (game.Players:GetChildren()) do local huntercount = replicatedstorage:WaitForChild("HunterCount") if huntercount.Value <= 0 then huntercount.Value += 1 plr.Character.PlayerTeam.Value = "Hunters" plr.Character.HumanoidRootPart.CFrame = CFrame.new(gamelocation) plr.Team = game.Teams.Hunters else propcount.Value += 1 plr.Character.PlayerTeam.Value = "Props" plr.Character.HumanoidRootPart.CFrame = CFrame.new(gamelocation) plr.Team = game.Teams.Props end end end local function resetall() for _, plr in pairs(game.Players:GetChildren()) do local huntercount = replicatedstorage:WaitForChild("HunterCount") huntercount.Value = 0 propcount.Value = 0 plr.Character.Humanoid.Health = 0 plr.Team = game.Teams.Lobby if plr.Team == game.Teams.Hunters then local propgun = plr:FindFirstChild("PropGun") or plr.Character:FindFirstChild("PropGun") if propgun then propgun:Destroy() end end end end local function hunterwait() for _, plr in pairs (game.Players:GetChildren()) do if plr.Team == game.Teams.Hunters then local hunterscreen = plr.PlayerGui:WaitForChild("HunterScreen") hunterscreen.Enabled = true wait(5) hunterscreen.Enabled = false local propgun = replicatedstorage:WaitForChild("PropGun") local gunclone = propgun:Clone() gunclone.Parent = plr.Backpack end end end while true do resetplayers() playintermission() teleportplayers() hunterwait() playgame() print("GAME OVER") resetall() end
Instead of breaking just call the function try this!
local lobbylocation = workspace.Lobby.Lobby.Position + Vector3.new(0, 3, 0) local gamelocation = workspace.Maps.Main.Position + Vector3.new(0, 3, 0) local replicatedstorage = game:GetService("ReplicatedStorage") local timeevent = replicatedstorage:WaitForChild("TimeEvent") local propcount = replicatedstorage.PropCount
wait(3) print("everything loaded")
game.Players.PlayerAdded:Connect(function(plr) plr.Team = game.Teams.Lobby replicatedstorage.ServerCount.Value += 1 end)
game.Players.PlayerRemoving:Connect(function() replicatedstorage.ServerCount.Value -= 1 end)
wait(1)
local function playgame()--300 local timeAmount = 20 local timerText = "Time Left: " while timeAmount > 0 and replicatedstorage.PropCount ~= 0 do timeevent:FireAllClients(timeAmount, timerText) wait(1) timeAmount -= 1 if replicatedstorage.PropCount == 0 then print("HUNTERS WIN") resetall()
if timeAmount == 0 then print("PROPS WIN") resetall() end end
end end
local function playintermission() local intermission = 10--60 local timerText = "Intermission: " while intermission > 0 do timeevent:FireAllClients(intermission, timerText) wait(1) intermission -= 1 end end
local function resetplayers() for _, plr in pairs (game.Players:GetChildren()) do plr.Character.HumanoidRootPart.CFrame = CFrame.new(lobbylocation) end end
local function teleportplayers() for _, plr in pairs (game.Players:GetChildren()) do local huntercount = replicatedstorage:WaitForChild("HunterCount") if huntercount.Value <= 0 then huntercount.Value += 1 plr.Character.PlayerTeam.Value = "Hunters" plr.Character.HumanoidRootPart.CFrame = CFrame.new(gamelocation) plr.Team = game.Teams.Hunters else propcount.Value += 1 plr.Character.PlayerTeam.Value = "Props" plr.Character.HumanoidRootPart.CFrame = CFrame.new(gamelocation) plr.Team = game.Teams.Props end
end
end
local function resetall() for _, plr in pairs(game.Players:GetChildren()) do local huntercount = replicatedstorage:WaitForChild("HunterCount") huntercount.Value = 0 propcount.Value = 0 plr.Character.Humanoid.Health = 0 plr.Team = game.Teams.Lobby if plr.Team == game.Teams.Hunters then local propgun = plr:FindFirstChild("PropGun") or plr.Character:FindFirstChild("PropGun") if propgun then propgun:Destroy() end end end end
local function hunterwait() for _, plr in pairs (game.Players:GetChildren()) do if plr.Team == game.Teams.Hunters then local hunterscreen = plr.PlayerGui:WaitForChild("HunterScreen") hunterscreen.Enabled = true wait(5) hunterscreen.Enabled = false local propgun = replicatedstorage:WaitForChild("PropGun") local gunclone = propgun:Clone() gunclone.Parent = plr.Backpack end end end
while true do resetplayers() playintermission() teleportplayers() hunterwait() playgame() print("GAME OVER") resetall() end
LOOPS
The break command is used to break out of a loop, such as for loops and while loops.
While loop
while true do if 1 == 1 then print("True") break else print("looping") end end print("broke out of loop and script is continuing")
What this code does is if 1 = 1 then
, it will print "True" and, it will break out of the loop otherwise, it just loops.
while loops yield the current thread meaning, it just halts forever, but if you use coroutines, your script can multi-task, but the while loop has to be in a function.
For loop
for i, v in ipairs(workspace:GetDescendants()) do if v:IsA("Script") then print(v.Name) break end end
What this code does is it will loop through all the descendants of the workspace, if it comes across a Script, it will print the name of the script and break out of the loop and continue.
for loops are like while loops, it yields the current thread. There was another way of looping through a table which was the table.foreach(dictionary)
and table.foreachi(array)
. These table functions didn't yield, so it's like a for loop except it doesn't yield, but these are deprecated now, so it's not recommended to use.
for loops will only work if ipairs or pairs have a table in the parenthesis. ipairs(array)
and pairs(dictionary)
.
functions such as GetChildren() and GetDescendants() returns an array.
Notes:
ipairs
is used for arrays.
pairs
is used for dictionaries.
RETURN
local function RNG() local randInt = math.random(1, 100) return randomInt end print(RNG())
RNG means random number generator.
So, what this does is it generates a number between 1 and 100 then, it returns the result of the randInt
then when I print the function it gives me a number between 1 and 100.
I was confused with return
before, but when I played around with it, I understood how it works. So, you should play around with it as I did.
Any misinformation? Just reply below. I know this isn't an answer, but it should help you understand a bit.