#How would I end a function/return to the top if conditions are met at any point within it?

1 messages · Page 1 of 1 (latest)

civic parcel
#

Trying to make a rounds system where if the players goes under 2 at any time it restarts to "Waiting for players..."

waxen whale
civic parcel
#

local Players = game:GetService("Players")
local started = game.ReplicatedStorage.RoundStarted
local ended = game.ReplicatedStorage.RoundEnded

Players.PlayerAdded:Connect(function(player)
local function round()
local text = player.PlayerGui:WaitForChild("ScreenGui"):WaitForChild("RoundsTextLabel")
local timer = player.PlayerGui:WaitForChild("ScreenGui"):WaitForChild("TimerTextLabel")
local character = player.Character
local humanoidRootPart = character.HumanoidRootPart
humanoidRootPart.CFrame = CFrame.new(game.Workspace.SpawnLocation.Position.X, game.Workspace.SpawnLocation.Position.Y + 3.5, game.Workspace.SpawnLocation.Position.Z)
text.Text = "Waiting for players..."
timer.Visible = false
local playercount = #Players:GetPlayers()
repeat
wait()
until playercount >= 2 or game:GetService("RunService"):IsStudio()
timer.Visible = true
text.Text = "Intermission..."
for i = 20, 0, -1 do
timer.Text = i
task.wait(1)
end
timer.Visible = false
text.Text = "Round started!"
task.wait(2)
timer.Visible = true
humanoidRootPart.CFrame = game.Workspace.RoundSpawnPart.CFrame
text.Text = "Round ongoing..."
started:Fire()
for i = 180, 0, -1 do
timer.Text = i
task.wait(1)
end
ended:Fire()
text.Text = "Round ended!"
task.wait(2)
end
while true do
round()
end
end)

waxen whale
civic parcel
#

waits until the player count is >= 2?

waxen whale
civic parcel
#

yeah i know

#

thats why im asking

night goblet
#
local Players = game:GetService("Players")
local maxPlayers = 2 -- change to whatever

local function checkPlayerAmount()
    if #Players:GetPlayers() <= maxPlayers then
        -- player amount is less than max players
end

Players.PlayerAdded:Connect(checkPlayerAmount)
Players.PlayerRemoving:Connect(checkPlayerAmount)
#

i think

#

this

waxen whale
night goblet
#

should be alr

civic parcel
#

i know the fundamentals

waxen whale
night goblet
#

reusable

#

functions

civic parcel
#

whoops

#

wrong script

waxen whale
#

you would have playeradded:connect(function(player) ... addplayertoround(player) ... etc ... end)

#

and in playerremoving removeplayerfromround(player) ... function removeplayerfromround(plr) numplayers -= 1; checkplayercount() end etc

waxen whale
civic parcel
#

error

night goblet
#

uh

waxen whale
night goblet
#

yeah

#

its like

#

example code

#

you can make a func connection

waxen whale
night goblet
#

then add multiple stuff

#

in that

waxen whale
#

this is scripting-help not free-scripts hehe

civic parcel
#

idk how to make it work at any time in the function

night goblet
#
Players.PlayerAdded:Connect(function()
    if checkPlayerAmount() then -- make the function return true or false
    end
end)
#

another way

civic parcel
#

because i have to call it manually

night goblet
#

to do

waxen whale
night goblet
#
local Players = game:GetService("Players")
local maxPlayers = 2 -- change to whatever

local function checkPlayerAmount()
    if #Players:GetPlayers() <= maxPlayers then
        -- player amount is less than max players
        return false
    else
       return true
    end
end

#

thats a nice way of doing imo

#

i like making check functions

#

it makes code look clean

waxen whale
night goblet
civic parcel
#

how would i stop the top function

waxen whale
night goblet
#

so many docs 😭

#

but its neccesary

waxen whale
# night goblet so many docs 😭

Provides a global registry of named Datatype.SharedTable objects.

Represents a reference to asset content stored externally or as an object
within the place.

night goblet
#

like what works

waxen whale
#

preferably game mechanics

night goblet
#

okay ill try find smth

waxen whale
#

not like 4 scripts each with one function in it

night goblet
#

would

#

modules be alright?

civic parcel
#

man idk

#

i've been tryna figure this out for so long

#

local Players = game:GetService("Players")
local started = game.ReplicatedStorage.RoundStarted
local ended = game.ReplicatedStorage.RoundEnded

Players.PlayerAdded:Connect(function(player)
local function round()
local text = player.PlayerGui:WaitForChild("ScreenGui"):WaitForChild("RoundsTextLabel")
local timer = player.PlayerGui:WaitForChild("ScreenGui"):WaitForChild("TimerTextLabel")
local character = player.Character
local humanoidRootPart = character.HumanoidRootPart
humanoidRootPart.CFrame = CFrame.new(game.Workspace.SpawnLocation.Position.X, game.Workspace.SpawnLocation.Position.Y + 3.5, game.Workspace.SpawnLocation.Position.Z)
text.Text = "Waiting for players..."
timer.Visible = false
local playercount = #Players:GetPlayers()
repeat
wait()
until playercount >= 2 or game:GetService("RunService"):IsStudio()
timer.Visible = true
text.Text = "Intermission..."
for i = 20, 0, -1 do
timer.Text = i
task.wait(1)
end
timer.Visible = false
text.Text = "Round started!"
task.wait(2)
timer.Visible = true
humanoidRootPart.CFrame = game.Workspace.RoundSpawnPart.CFrame
text.Text = "Round ongoing..."
started:Fire()
for i = 180, 0, -1 do
timer.Text = i
task.wait(1)
end
ended:Fire()
text.Text = "Round ended!"
task.wait(2)
end
while true do
round()
end
end)

waxen whale
night goblet
civic parcel
#

because i have to do return it inside the function but i want it to return whenever it goes under 2

night goblet
#

my framework would be alright

civic parcel
#

but other things are happening

waxen whale