Script not running in the correct order?

I’m trying to get all players to present on the runway, but it’s not working properly. This works by setting the status (a) to PLAYERNAME is on the runway!, then teleports the character to the runway, counts their score, and updates the count, if it is above zero say “Next up is…” and then it just goes straight to RunwayOver() and says all contestants have presented but there are one or more contestants that have not presented and their value of HasPresented is not set to true. Here are the relevant functions.

This is really bad coding so excuse the horrible practices, I’ll fix them once i actually get it to a working state :skull:

round()

	wait(2)
	a.Value = "Welcome!"
	wait(5)
	a.Value = "Choosing judges..."
	wait(5)
	chooseJudges()
	a.Value = "Judges Chosen!"
	wait(5)
	a.Value = "Moving players and judges..."
	wait(5)
	teleportJudges()
	teleportPlayers()
	wait(3)
	game.ReplicatedStorage.Countdown:FireAllClients() -- fire a remoteevent
	wait(3.0)
	a.Value = "Round Start!"
	--game.SoundService.airhorn:Play()
	for i,v in pairs(game.Workspace.room.Box:GetChildren()) do
		v.Transparency = 1
		v.CanCollide = false
	end
	local timer = 10
	game.ReplicatedStorage.IsTimer.Value = true
	repeat 
		timer = timer - 1
		a.Value = string.format("%d:%02d", math.floor(timer/60), timer%60)
		print(timer)
		wait(1)
	until timer == 0
	game.ReplicatedStorage.IsTimer.Value = false
	a.Value = "STOP!"
	game.SoundService.whistle:Play() -- Replace this later with a localized version (new remote or smth)
	for i,v in pairs(game.Players:GetPlayers()) do
		if v.Team == teams.Contestants then
			v.Character.HumanoidRootPart.Anchored = true
		end
	end
	wait(2)
	a.Value = "Teleporting Players..."
	wait(1)
	for i,v in pairs(game.Players:GetPlayers()) do
		if v.Team == teams.Contestants then
			v.Character.HumanoidRootPart.Anchored = false
		end
	end
	for i,v in pairs(game.Players:GetPlayers()) do
		if v.Team == teams.Contestants then
			v.Character:WaitForChild("HumanoidRootPart").CFrame = game.Workspace.Stage.backstage.tele.Attachment.WorldCFrame
		end
	end
	wait(3)
	runway()
end```
**RunwayOver()**
```function RunwayOver()
	if stop == false then
		stop = true
		a.Value = "All contestants have walked the runway!"
		game.SoundService.victory:Play()

		-- Move contestants to random floor points
		for _, v in pairs(game.Players:GetPlayers()) do
			if v.Team == teams.Contestants then
				local Floorpoints = game.Workspace.Stage.Flooring:GetChildren()
				local Chosen = Floorpoints[math.random(1, #Floorpoints)]
				v.Character.HumanoidRootPart.Position = Chosen.WorldPosition
			end
		end

		wait(5)
		a.Value = "Counting score..."

		local ScoreTable = {}
		for _, v in pairs(game.ReplicatedStorage.FinalScores:GetChildren()) do
			table.insert(ScoreTable, v.Name)
			table.insert(ScoreTable, v.Value)
		end

		print(ScoreTable)

		local function extractNumbers(tbl)
			local numbers = {}

			for _, value in ipairs(tbl) do
				local numValue = tonumber(value)
				if numValue then
					table.insert(numbers, numValue)
				end
			end

			return numbers
		end

		local numbersOnly = extractNumbers(ScoreTable)
		local highestNumber = math.max(table.unpack(numbersOnly))
		print("Highest number is " .. highestNumber)
		print("which has table index number " .. table.find(ScoreTable, highestNumber))
		local highestnumberindex = table.find(ScoreTable, highestNumber)
		local playernameindex = highestnumberindex - 1

		a.Value = "And the winner is..."
		game.Workspace.TEMP.Drumroll:Play()
		wait(5)
		a.Value = ScoreTable[playernameindex] .. "!"
		game.Workspace.TEMP["Who's The Winner? (e)"]:Play()
	end
end

CountScore()

function CountScore(player)
	local JudgeTotals = 0
	for _, judge in pairs(game.Players:GetChildren()) do
		if judge.Team == teams.Judge then
			local contestantVotes = judge.ContestantVotes:FindFirstChild(player.Name)
			if contestantVotes then
				local PlayerValue = contestantVotes.Value
				JudgeTotals = JudgeTotals + PlayerValue
			else
				warn("ContestantVotes child not found for player " .. player.Name .. " in judge " .. judge.Name)
			end
		end
	end
	return JudgeTotals
end

getCount()

	local localCount = 0
	for _, v in pairs(game.Players:GetPlayers()) do
		if v.Team == teams.Contestants and not v.HasPresented.Value then
			localCount = localCount + 1
			warn(localCount .. " THIS IS THE LOCALCOUNT")
		end
	end
	return localCount
end
2 Likes

What part of it isn’t working? You can also do workspace instead of game.Workspace. You can also do team:GetPlayers() instead of looping through players and comparing teams.

1 Like

The issue is that the first player gets to present, then it automatically goes straight to the message about all players having presented despite multiple players not having presented and their HasPresented value being false

Could you repaste your code? It looks like it’s not formatted properly and a decent chunk of it is missing.

1 Like

Here’s the entire script.

-- 2 judges 4 players
-- 3 judges 5 players+
-- 1 judge for 3 players?

local teams = game:GetService("Teams")
local roundtime = 200
local inter = 20
local a = game.ReplicatedStorage:WaitForChild("currentAnnouncement")
local pc = game.ReplicatedStorage.PlayerCount.Value
a.Value = "Initializing..."
local judge1 = nil
local judge2 = nil
local judge3 = nil
count = 0
function getCount()
	local localCount = 0
	for _, v in pairs(game.Players:GetPlayers()) do
		if v.Team == teams.Contestants and not v.HasPresented.Value then
			localCount = localCount + 1
			warn(localCount .. " THIS IS THE LOCALCOUNT")
		end
	end
	return localCount
end


function createContestantTable(judge)
	for i,v in pairs(game.Players:GetChildren()) do
		if v.Team == teams.Contestants then
			local NumVal = Instance.new("NumberValue")
			NumVal.Name = judge.Name
			NumVal.Parent = judge:WaitForChild("ContestantVotes")
		end
	end
end
function chooseJudges()
	local pc = game.ReplicatedStorage.PlayerCount.Value
	print("RunningFUN")

	if pc == 1 then
		print("pc is 1")
		local potentialJudges = game.Players:GetPlayers()
		for i, v in pairs(potentialJudges) do
			v.Team = game.Teams.Contestants
		end
	elseif pc == 2 then
		print("DebugTesterDetected")
		local num = 0
		for i, v in pairs(game.Players:GetChildren()) do
			if num == 0 then
				v.Team = teams.Contestants
				num = 1
			else
				v.Team = teams.Judge
				createContestantTable(v)
			end
		end
	elseif pc == 3 then
		print("3 Players, choose a judge.")
		local Contestants = game.Players:GetPlayers()
		local selected = Contestants[math.random(1, #Contestants)]
		local judges = { selected }
		print(judges)
		print(selected)
		print(Contestants)
		for i, v in pairs(Contestants) do
			v.Team = game.Teams.Contestants
			print("sad")
		end
		for i, v in pairs(judges) do
			v.Team = teams.Judge
			print(v.Name)
			print("That's the judge")
		end
		local judge = game.Players:GetPlayers()[math.random(1, #game.Players:GetPlayers())]
		print(judge.Name)
	elseif pc >= 4 then -- 2 or 3 judges
		local potentialJudges = game.Players:GetPlayers()
		local selected1 = potentialJudges[math.random(1, #potentialJudges)]
		table.remove(potentialJudges, table.find(potentialJudges, selected1))
		local selected2 = potentialJudges[math.random(1, #potentialJudges)]
		table.remove(potentialJudges, table.find(potentialJudges, selected2))

		-- Additional judge for pc >= 5
		local selected3 = nil
		if pc >= 5 then
			selected3 = potentialJudges[math.random(1, #potentialJudges)]
			table.remove(potentialJudges, table.find(potentialJudges, selected3))
		end

		for i, v in pairs(potentialJudges) do
			v.Team = game.Teams.Contestants
			createContestantTable(v)
		end

		selected1.Team = teams.Judge
		selected2.Team = teams.Judge
		if selected3 then
			selected3.Team = teams.Judge
		end
	else
		print(pc)
		error("Invalid player count for judging.")
	end
end

function teleportJudges()
	for i,v in pairs(game.Players:GetPlayers()) do
		if v.Team == teams.Judge then
			local bartpoints = game.Workspace.Stage.bart:GetChildren()
			local Chosen = bartpoints[math.random(1, #bartpoints)]
			v.Character:WaitForChild("HumanoidRootPart").Position = Chosen.WorldPosition
		end

	end
end
function teleportPlayers()
	for i,v in pairs(game.Players:GetChildren()) do
		if v.Team == teams.Contestants then
			v.Character.Humanoid.Jump = true
			v.Character.HumanoidRootPart.CFrame = game.Workspace.room.teleportPart.w.WorldCFrame
		end
	end
end
function CountScore(player)
	local JudgeTotals = 0
	for _, judge in pairs(game.Players:GetChildren()) do
		if judge.Team == teams.Judge then
			local contestantVotes = judge.ContestantVotes:FindFirstChild(player.Name)
			if contestantVotes then
				local PlayerValue = contestantVotes.Value
				JudgeTotals = JudgeTotals + PlayerValue
			else
				warn("ContestantVotes child not found for player " .. player.Name .. " in judge " .. judge.Name)
			end
		end
	end
	return JudgeTotals
end


stop=false
function RunwayOver()
	if stop == false then
		stop = true
		a.Value = "All contestants have walked the runway!"
		game.SoundService.victory:Play()

		-- Move contestants to random floor points
		for _, v in pairs(game.Players:GetPlayers()) do
			if v.Team == teams.Contestants then
				local Floorpoints = game.Workspace.Stage.Flooring:GetChildren()
				local Chosen = Floorpoints[math.random(1, #Floorpoints)]
				v.Character.HumanoidRootPart.Position = Chosen.WorldPosition
			end
		end

		wait(5)
		a.Value = "Counting score..."

		local ScoreTable = {}
		for _, v in pairs(game.ReplicatedStorage.FinalScores:GetChildren()) do
			table.insert(ScoreTable, v.Name)
			table.insert(ScoreTable, v.Value)
		end

		print(ScoreTable)

		local function extractNumbers(tbl)
			local numbers = {}

			for _, value in ipairs(tbl) do
				local numValue = tonumber(value)
				if numValue then
					table.insert(numbers, numValue)
				end
			end

			return numbers
		end

		local numbersOnly = extractNumbers(ScoreTable)
		local highestNumber = math.max(table.unpack(numbersOnly))
		print("Highest number is " .. highestNumber)
		print("which has table index number " .. table.find(ScoreTable, highestNumber))
		local highestnumberindex = table.find(ScoreTable, highestNumber)
		local playernameindex = highestnumberindex - 1

		a.Value = "And the winner is..."
		game.Workspace.TEMP.Drumroll:Play()
		wait(5)
		a.Value = ScoreTable[playernameindex] .. "!"
		game.Workspace.TEMP["Who's The Winner? (e)"]:Play()
	end
end

runwayChooser = game.Players:GetPlayers()

function runway()
	local validPlayers = {}

	for _, v in ipairs(runwayChooser) do
		if v.Team == teams.Contestants and not v.HasPresented.Value then
			table.insert(validPlayers, v)
		end
	end

	if #validPlayers == 0 then
		-- Handle the case where there are no contestants left
		RunwayOver()
		return
	end

	for _, selected in ipairs(validPlayers) do
		a.Value = selected.Name .. " is on the runway!"
		game.ReplicatedStorage.CurrentPlayer.Value = selected
		selected.HasPresented.Value = true

		selected.Character.HumanoidRootPart.CFrame = game.Workspace.Stage.walk.start.WorldCFrame
		selected.Character.Humanoid:MoveTo(game.Workspace.Stage.walk.edge.WorldPosition)
		wait(10)

		local currentCount = getCount()
		local counted = CountScore(selected)
		local FinalScore = Instance.new("NumberValue")
		FinalScore.Value = counted
		FinalScore.Parent = game.ReplicatedStorage.FinalScores
		FinalScore.Name = selected.Name

		-- Update count after the presentation
		currentCount = getCount()

		if currentCount > 0 then
			a.Value = "Next up is..."
			wait(2)

			local Floorpoints = game.Workspace.Stage.Flooring:GetChildren()
			local Chosen = Floorpoints[math.random(1, #Floorpoints)]
			selected.Character.HumanoidRootPart.Position = Chosen.WorldPosition
			wait(2)
		end
	end

	-- Call the runway function again after all contestants have presented
	runway()
end
function round()
	wait(2)
	a.Value = "Welcome!"
	wait(5)
	a.Value = "Choosing judges..."
	wait(5)
	chooseJudges()
	a.Value = "Judges Chosen!"
	wait(5)
	a.Value = "Moving players and judges..."
	wait(5)
	teleportJudges()
	teleportPlayers()
	wait(3)
	game.ReplicatedStorage.Countdown:FireAllClients() -- fire a remoteevent
	wait(3.0)
	a.Value = "Round Start!"
	--game.SoundService.airhorn:Play()
	for i,v in pairs(game.Workspace.room.Box:GetChildren()) do
		v.Transparency = 1
		v.CanCollide = false
	end
	local timer = 10
	game.ReplicatedStorage.IsTimer.Value = true
	repeat 
		timer = timer - 1
		a.Value = string.format("%d:%02d", math.floor(timer/60), timer%60)
		print(timer)
		wait(1)
	until timer == 0
	game.ReplicatedStorage.IsTimer.Value = false
	a.Value = "STOP!"
	game.SoundService.whistle:Play() -- Replace this later with a localized version (new remote or smth)
	for i,v in pairs(game.Players:GetPlayers()) do
		if v.Team == teams.Contestants then
			v.Character.HumanoidRootPart.Anchored = true
		end
	end
	wait(2)
	a.Value = "Teleporting Players..."
	wait(1)
	for i,v in pairs(game.Players:GetPlayers()) do
		if v.Team == teams.Contestants then
			v.Character.HumanoidRootPart.Anchored = false
		end
	end
	for i,v in pairs(game.Players:GetPlayers()) do
		if v.Team == teams.Contestants then
			v.Character:WaitForChild("HumanoidRootPart").CFrame = game.Workspace.Stage.backstage.tele.Attachment.WorldCFrame
		end
	end
	wait(3)
	runway()
end



round()
1 Like

Are you intentionally calling runway after every member has presented?

1 Like

Yeah, it runs runway again for the next contestant

Your code already does every contestant before calling runway, your comment says that too. I think you meant to call runwayOver.

Code:

-- 2 judges 4 players
-- 3 judges 5 players+
-- 1 judge for 3 players?

local teams = game:GetService("Teams")
local roundtime = 200
local inter = 20
local a = game.ReplicatedStorage:WaitForChild("currentAnnouncement")
local pc = game.ReplicatedStorage.PlayerCount.Value
a.Value = "Initializing..."
local judge1 = nil
local judge2 = nil
local judge3 = nil
local count = 0
local function getCount()
	local localCount = 0
	for _, v in pairs(teams.Contestants:GetPlayers()) do
		if not v.HasPresented.Value then
			localCount += 1
			warn(localCount .. " THIS IS THE LOCALCOUNT")
		end
	end
	return localCount
end


local function createContestantTable(judge)
	for i,v in pairs(teams.Contestants:GetPlayers()) do
		local NumVal = Instance.new("NumberValue")
		NumVal.Name = judge.Name
		NumVal.Parent = judge:WaitForChild("ContestantVotes")
	end
end
local function chooseJudges()
	local pc = game.ReplicatedStorage.PlayerCount.Value
	print("RunningFUN")

	if pc == 1 then
		print("pc is 1")
		local potentialJudges = game.Players:GetPlayers()
		for i, v in pairs(potentialJudges) do
			v.Team = game.Teams.Contestants
		end
	elseif pc == 2 then
		print("DebugTesterDetected")
		local num = 0
		for i, v in pairs(game.Players:GetChildren()) do
			if num == 0 then
				v.Team = teams.Contestants
				num = 1
			else
				v.Team = teams.Judge
				createContestantTable(v)
			end
		end
	elseif pc == 3 then
		print("3 Players, choose a judge.")
		local Contestants = game.Players:GetPlayers()
		local selected = Contestants[math.random(1, #Contestants)]
		local judges = { selected }
		print(judges)
		print(selected)
		print(Contestants)
		for i, v in pairs(Contestants) do
			v.Team = game.Teams.Contestants
			print("sad")
		end
		for i, v in pairs(judges) do
			v.Team = teams.Judge
			print(v.Name)
			print("That's the judge")
		end
		local judge = game.Players:GetPlayers()[math.random(1, #game.Players:GetPlayers())]
		print(judge.Name)
	elseif pc >= 4 then -- 2 or 3 judges
		local potentialJudges = game.Players:GetPlayers()
		local selected1 = potentialJudges[math.random(1, #potentialJudges)]
		table.remove(potentialJudges, table.find(potentialJudges, selected1))
		local selected2 = potentialJudges[math.random(1, #potentialJudges)]
		table.remove(potentialJudges, table.find(potentialJudges, selected2))

		-- Additional judge for pc >= 5
		local selected3 = nil
		if pc >= 5 then
			selected3 = potentialJudges[math.random(1, #potentialJudges)]
			table.remove(potentialJudges, table.find(potentialJudges, selected3))
		end

		for i, v in pairs(potentialJudges) do
			v.Team = game.Teams.Contestants
			createContestantTable(v)
		end

		selected1.Team = teams.Judge
		selected2.Team = teams.Judge
		if selected3 then
			selected3.Team = teams.Judge
		end
	else
		print(pc)
		error("Invalid player count for judging.")
	end
end

local function teleportJudges()
	for i,v in pairs(teams.Judge:GetPlayers()) do
		local bartpoints = workspace.Stage.bart:GetChildren()
		local Chosen = bartpoints[math.random(1, #bartpoints)]
		v.Character:WaitForChild("HumanoidRootPart").Position = Chosen.WorldPosition
	end
end

local function teleportPlayers()
	for i,v in pairs(teams.Contestants:GetPlayers()) do
		v.Character.Humanoid.Jump = true
		v.Character.HumanoidRootPart.CFrame = workspace.room.teleportPart.w.WorldCFrame
	end
end
local function CountScore(player)
	local JudgeTotals = 0
	for _, judge in pairs(teams.Judge:GetPlayers()) do
		local contestantVotes = judge.ContestantVotes:FindFirstChild(player.Name)
		if contestantVotes then
			local PlayerValue = contestantVotes.Value
			JudgeTotals = JudgeTotals + PlayerValue
		else
			warn("ContestantVotes child not found for player " .. player.Name .. " in judge " .. judge.Name)
		end
	end
	return JudgeTotals
end

local function RunwayOver()
	a.Value = "All contestants have walked the runway!"
	game.SoundService.victory:Play()

	-- Move contestants to random floor points
	for _, v in pairs(teams.Contestants:GetPlayers()) do
		local Floorpoints = workspace.Stage.Flooring:GetChildren()
		local Chosen = Floorpoints[math.random(1, #Floorpoints)]
		v.Character.HumanoidRootPart.Position = Chosen.WorldPosition
	end

	wait(5)
	a.Value = "Counting score..."

	local ScoreTable = {}
	for _, v in pairs(game.ReplicatedStorage.FinalScores:GetChildren()) do
		table.insert(ScoreTable, v.Name)
		table.insert(ScoreTable, v.Value)
	end

	print(ScoreTable)

	local function extractNumbers(tbl)
		local numbers = {}

		for _, value in ipairs(tbl) do
			local numValue = tonumber(value)
			if numValue then
				table.insert(numbers, numValue)
			end
		end

		return numbers
	end

	local numbersOnly = extractNumbers(ScoreTable)
	local highestNumber = math.max(table.unpack(numbersOnly))
	print("Highest number is " .. highestNumber)
	print("which has table index number " .. table.find(ScoreTable, highestNumber))
	local highestnumberindex = table.find(ScoreTable, highestNumber)
	local playernameindex = highestnumberindex - 1

	a.Value = "And the winner is..."
	workspace.TEMP.Drumroll:Play()
	wait(5)
	a.Value = ScoreTable[playernameindex] .. "!"
	workspace.TEMP["Who's The Winner? (e)"]:Play()
end

local function runway()
	local validPlayers = {}

	for _, v in ipairs(teams.Contestants:GetPlayers()) do
		if not v.HasPresented.Value then
			table.insert(validPlayers, v)
		end
	end

	if #validPlayers == 0 then
		-- Handle the case where there are no contestants left
		RunwayOver()
		return
	end

	for _, selected in ipairs(validPlayers) do
		a.Value = selected.Name .. " is on the runway!"
		game.ReplicatedStorage.CurrentPlayer.Value = selected
		selected.HasPresented.Value = true

		selected.Character.HumanoidRootPart.CFrame = workspace.Stage.walk.start.WorldCFrame
		selected.Character.Humanoid:MoveTo(workspace.Stage.walk.edge.WorldPosition)
		wait(10)

		local counted = CountScore(selected)
		local FinalScore = Instance.new("NumberValue")
		FinalScore.Value = counted
		FinalScore.Name = selected.Name
		FinalScore.Parent = game.ReplicatedStorage.FinalScores

		-- Update count after the presentation
		local currentCount = getCount()

		if currentCount > 0 then
			a.Value = "Next up is..."
			wait(2)

			local Floorpoints = workspace.Stage.Flooring:GetChildren()
			local Chosen = Floorpoints[math.random(1, #Floorpoints)]
			selected.Character.HumanoidRootPart.Position = Chosen.WorldPosition
			wait(2)
		end
	end

	-- Call the runwayOver function again after all contestants have presented
	RunwayOver()
end

local function round()
	wait(2)
	a.Value = "Welcome!"
	wait(5)
	a.Value = "Choosing judges..."
	wait(5)
	chooseJudges()
	a.Value = "Judges Chosen!"
	wait(5)
	a.Value = "Moving players and judges..."
	wait(5)
	teleportJudges()
	teleportPlayers()
	wait(3)
	game.ReplicatedStorage.Countdown:FireAllClients() -- fire a remoteevent
	wait(3.0)
	a.Value = "Round Start!"
	--game.SoundService.airhorn:Play()
	for i,v in pairs(workspace.room.Box:GetChildren()) do
		v.Transparency = 1
		v.CanCollide = false
	end
	local timer = 10
	game.ReplicatedStorage.IsTimer.Value = true
	repeat 
		timer = timer - 1
		a.Value = string.format("%d:%02d", math.floor(timer/60), timer%60)
		print(timer)
		wait(1)
	until timer == 0
	game.ReplicatedStorage.IsTimer.Value = false
	a.Value = "STOP!"
	game.SoundService.whistle:Play() -- Replace this later with a localized version (new remote or smth)
	for i,v in pairs(teams.Contestants:GetPlayers()) do
		v.Character.HumanoidRootPart.Anchored = true
	end
	wait(2)
	a.Value = "Teleporting Players..."
	wait(1)
	for i,v in pairs(teams.Contestants:GetPlayers()) do
		v.Character.HumanoidRootPart.Anchored = false
	end
	for i,v in pairs(teams.Contestants:GetPlayers()) do
		v.Character:WaitForChild("HumanoidRootPart").CFrame = workspace.Stage.backstage.tele.Attachment.WorldCFrame
	end
	wait(3)
	runway()
end

round()
1 Like

download (1)

This seems to be working. Lemme do a bit more testing then i’ll mark as resolved

1 Like

By the way, you should be using task.wait for better efficiency and timing: Task.wait(n) vs wait(n)

1 Like

Thank you for finding my stupid mistake lmfao. You just saved my game

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.