Need help with a Bindable Event

Hello people! I’ve been working on a rounds system for my antigravity game when I found a problem I can’t quite solve.

This is my script:

local workspace = game.Workspace
local lobby = workspace.Lobby
local arena = workspace.Arena
local obstacles = arena.Obstacles
local projectiles = arena.Projectiles

local PlayerFolder = workspace.Players
local RedFolder = PlayerFolder.Red
local BlueFolder = PlayerFolder.Blue

local ContinueEvent = script:WaitForChild("Continue")

local TimerGui = game.ReplicatedStorage.UpdateGui.Timer
local DisplayGui = game.ReplicatedStorage.UpdateGui.Display

local teams = game.Teams
local blueTeam = teams.Blue
local redTeam = teams.Red
local lobbyTeam = teams.Lobby

local bot = game.ServerStorage.Assets.Bot

local players = game.Players

local RoundTime = 600
local IntermissionTime = 30

local redTeamCount = 0
local blueTeamCount = 0
local currentTime = 0

local function update()
	redTeamCount = #RedFolder.Active:GetChildren()
	blueTeamCount = #BlueFolder.Active:GetChildren()
	if redTeamCount == 0 or blueTeamCount == 0 then
		ContinueEvent:Fire()
		print("continue fired")
	end
end

update()
RedFolder.Active.ChildAdded:Connect(update)
BlueFolder.Active.ChildAdded:Connect(update)
RedFolder.Active.ChildRemoved:Connect(update)
BlueFolder.Active.ChildRemoved:Connect(update)

local function timer(TotalTime)
	for i = TotalTime, 0, -1 do
		currentTime = i
		
		local minutes = math.floor(i/60)
		local seconds = math.floor(i%60)
		if seconds < 10 then
			seconds = "0".. tostring(math.floor(i%60))
		end
		if minutes < 10 then
			minutes = "0".. tostring(math.floor(i/60))
		end
		TimerGui.Value = minutes .. ":" .. seconds
		wait(1)
		if redTeamCount == 0 or blueTeamCount == 0 then
			break
		end
	end
	ContinueEvent:Fire()
end

local function TwoTeams()
	local playersTable = players:GetPlayers()
	local playerNumber = #playersTable
	local TotalRedTeamNumber = math.floor((playerNumber + 1) / 2)
	for i, player in pairs(playersTable) do
		player:LoadCharacter()
		if i < (TotalRedTeamNumber + 1) then
			player.Team = redTeam
			player.Character.Parent = PlayerFolder[player.Team.Name].Active
			local spawnNumber = 1
			if i > 4 then
				spawnNumber = i - 4
			end

			local character = player.Character or player.CharacterAdded:Wait()
			local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
			if humanoidRootPart then
				character.CableHook.Transparency = 1
				character.CableHook.Cable.Enabled = false
				character.CableHook.Weld.Part1 = nil
				character.CableHook.Anchored = false
				
				character:MoveTo(obstacles.Red[spawnNumber].SpawnLocation.Position)
			end
		end
	end
	if playerNumber % 2 == 1 then
		local botClone = bot:Clone()
		botClone.Parent = BlueFolder.Active
	end
	timer(300)
end

redTeam.PlayerAdded:Connect(function()
	
end)

wait(10)

while true do
	TwoTeams()
	
	ContinueEvent.Event:Wait()
	print("Time Left:" .. currentTime .. "Red Players Left:" .. redTeamCount .. "Blue Players Left:" .. blueTeamCount)
	if currentTime == 0 then
		print("timer ended")
		DisplayGui.Value = "Draw!"
	elseif redTeamCount == 0 then
		print("Blue Team Wins!")
		DisplayGui.Value = "Blue Team Wins!"
	elseif blueTeamCount == 0 then
		print("Red Team Wins!")
		DisplayGui.Value = "Red Team Wins!"
	end
end

There are no errors in the output, but the print("Time Left:" .. currentTime .. "Red Players Left:" .. redTeamCount .. "Blue Players Left:" .. blueTeamCount) doesn’t print, and nothing after that prints either.

1 Like

Also, for some reason, when I click the stop button on studio, it prints “Blue Team Wins!”

I don’t think that matters too much currently. Let’s try and solve this, then deal with that when the time comes. :wink:

1 Like

Can you please show me more of your workspace? You have all these folders, I just want to know more about where they are and their positioning.

Alright, here you go!


I have a separate script that teleports players and bots to the “players” folder, and I can confirm it works, and has been working for the past few weeks.

Just want to note this, the workspace can be referenced by simple saying workspace.whateveryourtryingtoget

Oh, yeah, I know that works, but it looks… I dunno, unsophisticated? I just prefer doing game.Workspace for some reason :sweat_smile:

I will continue helping with this some more tomorrow, I just need to look through the code.

Alright, I’ll work on the intermission system for now :+1:

1 Like

I found the problem! I discovered that when you call a function, if that function has yielding in it, the current thread will also yield, so the Event:Wait() only occurs after the timer ends, or in other words, once the round is finished.
Thanks for all your help, and yet again… I have wasted several days of my life on a random stupid mistake.

Great job! Sorry I didn’t beat you to it :rofl: let me know if you need any help, I am in your debt.

Your game sounds really fun! :wink:

Lol I just realized you’re the same person I helped with the tower game:D
Also, I do not accept debt :smiling_imp: lol.
No need to give credit, but if you want, you can.

Oh haha I thought you helped me because you went to my project. I noticed the deja vu.