:Wait() connected to a function doesn't yield the third time

Hello, basically for the game the system I have created is supposed to do as follows:

  1. The same image appears on multiple players screens (people inside of the player table)
  2. When a player types the correct answer to the image on their screen in chat, the round ends for all other players
  3. All players have their image and swapped to the next one on the list (and correct answer as well), and the cycle continues for a predetermined amount of times.

In order to accomplish this:

I first activate a remote event (server → client) to the certain players i want for the round, the remote event changes the players gui, and allows them to have their answer count when typed in chat.
When the correct answer is typed, another remote event is fired (client to server) and that remote event alerts all other players that someone has typed the correct answer, and forwards the round.

This is the local script:

wait(1)
RepStorage = game:GetService("ReplicatedStorage")
plr = game.Players.LocalPlayer

plrGui = plr.PlayerGui
plrImageHolder = plrGui:WaitForChild("GameGui"):WaitForChild("Frame"):WaitForChild("GuessItem")

Events = RepStorage:WaitForChild("Events")
RoundTick = Events:WaitForChild("SendRound")
RoundComplete = Events:WaitForChild("RoundComplete")

local function OnTick(item)
	itemName = string.lower(item.Name)
	plrImageHolder.Image = item.Image
	print("Item sent: ", itemName)
	
	RoundComplete.OnClientEvent:Wait()
	itemName = nil
	print("Round Ended")
end

plr.Chatted:Connect(function(message)
	print(string.lower(message))
	if string.lower(message) == itemName then
		RoundComplete:FireServer()
	end
end)

RoundTick.OnClientEvent:Connect(OnTick)

This is (part) of the server script

local function roundBegin(playerFolder, roundLength, fightFolder)
	local list = listGenerate(fightFolder, RoundCount)
	-- printItemList(list, fightFolder.Parent.Name)

	for i = 1, roundLength do
		print("Round", i)

		local function OnComplete(player)
			print(player, "scored")

			for j, v in pairs(playerFolder) do --Alert other players someone has scored
				RoundComplete:FireClient(playerTable[j])
			end

		end
			
		RoundComplete.OnServerEvent:Connect(OnComplete)


		for k, v in pairs(playerFolder) do
			SendRound:FireClient(playerFolder[k], list[i])
		end	

		RoundComplete.OnServerEvent:Wait()
		wait(RoundCooldown)
	end
end

wait(2)
playerTable = {game.Players["50ShadesOfLamps"]}
roundBegin(playerTable, RoundCount, fightFolder)

The problem is the line in the client script : “RoundComplete.OnClientEvent:Wait()”
It works fine the first 2 rounds of the match, but on the third, the script does not yield for the event to be called and flies right through, and stops the match from continuing. It is odd because the roundComplete function never even fires for a third time. (Which is also why the next round doesn’t start because the server script waits for the remote event to be fired before the next round will be began)

I have tried googling, and I have even tried ChatGPT and its come up dry.
Thanks for anyone who decides to help me.

Hello, Bubby I believe I know what the issue your facing is. So, one reason could be that the event is not being reset or handled correctly after each round. Check the event handling in both the client and server scripts to make sure it is being managed correctly. Tell me if you have anymore problems.

This is the edited version, I basically rewrote the function in order to be able to use :Disconnect on it . I was basically just trying random things. I do not understand why it works.

for i = 1, roundLength do
		print("Round", i)

		
		local OnComplete = RoundComplete.OnServerEvent:Connect(function()
			local function OnComplete(player)
				OnComplete:Disconnect()
				print(player, "scored")

				for j, v in pairs(playerFolder) do --Alert other players someone has scored
					RoundComplete:FireClient(playerTable[j])
				end

			end			
		end)
		

		for k, v in pairs(playerFolder) do
			SendRound:FireClient(playerFolder[k], list[i])
		end	
		
		RoundComplete.OnServerEvent:Wait()
		OnComplete:Disconnect()
		wait(RoundCooldown)
	end
end

If someone could explain why it works I would actually appreciate it.

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