How can I prevent this infinite yield?

I’m making this check-in GUI where there are three buttons. When you press one of them, it gives you your ticket and I’m trying to code my script to make the text say that the player already has a ticket when they come back.

But the problem is that there’s this infinite yield warning I keep getting.

I placed my code in a local script:

local button = script.Parent.TextButton

local regularText = "Welcome!"

local OkButton = script.Parent.OkButton
OkButton.Visible = false

local ticketReceived = game.ReplicatedStorage["Check-In Events"]:WaitForChild("AmericanCheck-In")

local ticket1 = game.ReplicatedStorage.Tickets["Economy Ticket"]
local ticket2 = game.ReplicatedStorage.Tickets["Club Ticket"]
local ticket3 = game.ReplicatedStorage.Tickets["First Class Ticket"]

local class1 = script.Parent["Seating Classes1"]
local class2 = script.Parent["Seating Classes2"]
local class3 = script.Parent["Seating Classes3"]

local freeButton = script.Parent.Free
local groupButton = script.Parent.Group
local passButton = script.Parent.Pass

local thingsToSay = {"Have a nice flight!"; "Enjoy your trip!"; "Safe travels!"}

local buttons = {class1, class2, class3, freeButton, groupButton, passButton}

local gamepassID = 44128567
local mps = game:GetService("MarketplaceService")

for i, v in pairs(buttons) do
	v.Visible = false
end

button.MouseButton1Click:Connect(function()
	button.Visible = false
	script.Parent.TextLabel:TweenSize(UDim2.new(0, 400, 0, 45), 0.75)
	wait(0.75)
	for i, v in pairs(buttons) do
		v.Visible = true
	end
end)

local player = game.Players.LocalPlayer

local ticketEquipped = {player.Backpack:WaitForChild("Economy Ticket");
	player.Backpack:WaitForChild("Club Ticket");
	player.Backpack:WaitForChild("First Class Ticket")
}


freeButton.MouseButton1Click:Connect(function()
	ticket1:Clone().Parent = player:WaitForChild("Backpack")
	for i, v in pairs(buttons) do
		v.Visible = false
	end
	script.Parent.TextLabel.Size = UDim2.new(0, 400, 0, 100)
	script.Parent.TextLabel.Text = "You have received your ticket. "..thingsToSay[math.random(1, #thingsToSay)]
	OkButton.Visible = true
	
	ticketReceived:FireServer(player, ticket1)
end)

groupButton.MouseButton1Click:Connect(function()
	
	if player:IsInGroup(12749630) then
		ticket2:Clone().Parent = player:WaitForChild("Backpack")
		for i, v in pairs(buttons) do
			v.Visible = false
		end
		script.Parent.TextLabel.Text = "You have received your ticket. "..math.random(1, #thingsToSay)
		OkButton.Visible = true
	else
		script.Parent.TextLabel.Text = "Sorry, you need to join the group to access this."
		OkButton.Visible = true
		for i, v in pairs(buttons) do
			v.Visible = false
		end
	end
	
	ticketReceived:FireServer(player, ticket2)
end)

passButton.MouseButton1Click:Connect(function()
	if mps:UserOwnsGamePassAsync(player.UserId, gamepassID) then
		
		ticket3:Clone().Parent = player:WaitForChild("Backpack")
		for i, v in pairs(buttons) do
			v.Visible = false
		end
		script.Parent.TextLabel.Text = "You have received your ticket. "..math.random(1, #thingsToSay)
		OkButton.Visible = true
		
		ticketReceived:FireServer(player, ticket3, mps, gamepassID)
		
	else
		
		for i, v in pairs(buttons) do
			v.Visible = false
		end
		
		script.Parent.TextLabel.Text = "Sorry, you must require a gamepass to buy this ticket!"
		OkButton.Visible = true
		
		mps:PromptGamePassPurchase(player, gamepassID)
		
	end
end)

OkButton.MouseButton1Click:Connect(function()
	script.Parent.Visible = false
	OkButton.Visible = false
	
end)

workspace["Check-In Desk 1"].Union1["Check-In"].Triggered:Connect(function()
	
	script.Parent.Visible = true
	script.Parent.TextLabel.Size = UDim2.new(0, 400, 0, 100)
	for _, alreadyOwned in pairs(ticketEquipped) do
		if alreadyOwned then
			script.Parent.TextLabel.Text = "You already have a ticket!"
			button.Visible = false
			OkButton.Visible = true
		else
			script.Parent.TextLabel.Text = regularText
			button.Visible = true
			OkButton.Visible = false 
		end
	end
	
end)

workspace["Check-In Desk 1"].Union2["Check-In"].Triggered:Connect(function()
	script.Parent.Visible = true
	script.Parent.TextLabel.Size = UDim2.new(0, 400, 0, 100)
	for _, alreadyOwned in pairs(ticketEquipped) do
		if alreadyOwned then
			script.Parent.TextLabel.Text = "You already have a ticket!"
			button.Visible = false
			OkButton.Visible = true
		else
			script.Parent.TextLabel.Text = regularText
			button.Visible = true
			OkButton.Visible = false
		end
	end
end)

It would be very appreciating if you can fix this infinite yield.

1 Like

Try changing the WaitForChild to FindFirstChild. If that doesn’t work, you can instead do

for _,ticket in ipairs(ticketEquipped) do
    
    repeat wait() until player.Backpack:FindFirstChild(tool.Name)
end

The code above should be placed below the ticketEquipped table.

1 Like

Thanks for posting. The infinite yield warning stopped.

2 Likes

Just curious, which one worked?

The one with FindFirstChild. Haven’t tried the repeat loop yet.