Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I stop a function when a value reaches a certain number?

Asked by 3 years ago

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
0
Umm why would you want to break out of a function, you normally break out of loops.. sne_123456 439 — 3y
0
I'm new to round systems and stuff, and I just want it so that when the propcount value reaches 0 it moves onto the resetall() function, and I have no idea how. TabooAntonioiscool 42 — 3y
0
why do u have a while true do at the end im confused sne_123456 439 — 3y
0
so that it plays the round over and over again - it's a prophunt game. all i want is that when the props all get killed or when the hunters run out of time that the loop plays from the start but idk how to make it restart TabooAntonioiscool 42 — 3y
View all comments (8 more)
0
Hi thanks for your comment, are there any errors? sne_123456 439 — 3y
0
no errors but it's still not working, i added prints and theyre not activating (after it says if propcount == 0 then i added a print and when it does equal zero it never does anything or prints anything) TabooAntonioiscool 42 — 3y
0
so it doesnt even print hunters win and stuff? sne_123456 439 — 3y
0
nope nothin TabooAntonioiscool 42 — 3y
0
well that means your script isnt coded correctly sne_123456 439 — 3y
0
well shit lol TabooAntonioiscool 42 — 3y
0
Hi can you add me to team create so i can try solve this sne_123456 439 — 3y
0
sure thing TabooAntonioiscool 42 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Please encode Lua code in the Lua block code tag (look for the Lua icon in the editor).

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

0
hmm still not working - the reset function isnt even triggering for some reason TabooAntonioiscool 42 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

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.

0
thanks for the info! ill for sure refer to this when i need, thanks for the clarification. TabooAntonioiscool 42 — 3y

Answer this question