"for" loop halts at the second instance(solved)

here`s the problem:
I tried to turn a tycoon into a package to simplify the development. after that, i made changes into script and now tycoon does not load more than 2 objects

console log:

tycoonManager:

local tycoon = script.Parent
local buttons = tycoon.TYCOON_BUTTONS
local objects = tycoon.TYCOON_OBJECTS
local sound = script.buildsound
local stat = tycoon.incomeStats
local buttonOriginTransparency = buttons['Concrete base'].Transparency
local SerStor = game.ServerStorage

wait(.5)
--delete objects
function clearObjects()
	for a, d in pairs(objects:GetChildren()) do
		d:Destroy()
	end
	print('deleted')
end
function removeObjects()
	for i,v in pairs(objects:GetChildren()) do
		v.Parent = SerStor.tycoons[tycoon.Name].TYCOON_OBJECTS
		task.wait()
	end
	print('hidden in storage')
end
function checkUp(object, taebl)
	for i,v in pairs(object:GetChildren()) do
		if v.ClassName == 'Part' or v.ClassName == 'UnionOperation' or v.ClassName == 'MeshPart' then
			table.insert(taebl, v)	
		elseif v.ClassName == 'Model' then
			checkUp(v, taebl)
		end
	end
end

function animation(object, button)
	local clolne = script.animSound:Clone()
	clolne.Parent = button
	clolne:Play()
	
	local parts = {}
	checkUp(object, parts)
	local transparencyTable = {}
	local collisionTable = {}
	for t,r in pairs(parts) do
		table.insert(transparencyTable, r.Transparency)
		table.insert(collisionTable, r.CanCollide)
	end
	for a,d in pairs(parts) do
		
		d.Transparency = 1
		d.CanCollide = false
	end
	for i,v in pairs(parts) do
		
		local duration = 3 / #parts
		local tween = game.TweenService:Create(v, TweenInfo.new(duration, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {Transparency = transparencyTable[i], CFrame = v.CFrame})
		v.CFrame = tycoon.Owner.Value.Character.Head.CFrame
		v.CFrame *= CFrame.Angles(math.random(-180,180), math.random(-180,180), math.random(-180,180))
		tween:Play()
		tween.Completed:Connect(function()
			v.CanCollide = collisionTable[i]
		end)
		
		task.wait(duration * 0.5)
	end
	clolne:Destroy()
end

function addIncome(thingy)
	
	local modelItself = objects:FindFirstChild(thingy.Name)
	
	if modelItself:GetTags() ~= nil then
		if modelItself:GetTags()[1] == 'small pump' then
			stat.smallRigValue.Value += 1
		elseif modelItself:GetTags()[1] == 'bigger pump' then
			stat.biggerRigValue.Value += 1
		elseif modelItself:GetTags()[1] == 'research' then
			stat.researchValue.Value += 1
		end
	end
end

function toggleButton(button, enabled)
	
	if enabled == true then
		
		button.Transparency = buttonOriginTransparency
	else
		button.Transparency = 1
	end
	button.CanTouch = enabled
	button.ButtonBillboard.Enabled = enabled
	button.CanCollide = enabled
end
function updateButtons()
	--swipe buttons
	for i,v in pairs(buttons:GetChildren()) do
		
		
		local currentObject = objects:FindFirstChild(v.Name)
		
		--если не нужен родитель
		if v.par.Value == nil then
			
			--если ты купил кнопку, она исчезнет
			if currentObject then
				toggleButton(v, false)
			else
				toggleButton(v, true)
			end
		else --родитель нужен

			local parentObject = objects:FindFirstChild(v.par.Value.Name)
			--если родительский предмет куплен
			if parentObject then
				--если кнопка куплена, то избавиться
				if currentObject then
					toggleButton(v, false)
				else
					toggleButton(v, true)
				end
			else -- нет родителя - нет кнопки
				toggleButton(v, false)
			end
			
		end

	end
end
--buttons
for i,v in pairs(buttons:GetChildren()) do
	
	v.ButtonBillboard.TextLabel.Text = v.Name..' for '..v.cost.Value .. "$"
	v.ButtonBillboard.Enabled = true
	
	v.Touched:Connect(function(touch)
			
		local thisButton = v

		local player = game.Players:GetPlayerFromCharacter(touch.Parent)

		if tycoon.Owner.Value ~= nil then

			if player.Name == tycoon.Owner.Value.Name then

				local playerMoney = player.leaderstats.money
				local buttonCost = thisButton.cost.Value
				
				if playerMoney.Value >= buttonCost and objects:FindFirstChild(thisButton.Name) == nil then
					
					
					toggleButton(thisButton, false)
					
					playerMoney.Value -= buttonCost
					

					local object = SerStor.tycoons[tycoon.Name].TYCOON_OBJECTS:FindFirstChild(thisButton.Name)
					local clone = object:Clone()
					clone.Parent = objects
					addIncome(thisButton)
					
					--animation where clone object being constructed part by part
					task.spawn(animation, clone, thisButton)
				
					updateButtons()
					local sndClone = sound:Clone()
					sndClone.Parent = thisButton
					sndClone:Play()
					wait(1)
					sndClone:Destroy()

					local documentOfPurchasing = Instance.new('Configuration')
					documentOfPurchasing.Parent = player.PurchasedFolder
					documentOfPurchasing.Name = thisButton.Name
				end

			end
		end
		
	end)
end
wait()

removeObjects()
updateButtons()


function OwnerCheck(plr)
	if plr == nil then
		
		clearObjects()
		updateButtons()
	else
		
		print('owner changed')
		
		plr.RespawnLocation = tycoon.SpawnLocation
		
		--respawn character to make it spawn near tycoon
		local char = plr.Character
		char:WaitForChild('HumanoidRootPart').CFrame = tycoon.SpawnLocation.CFrame + Vector3.new(0,5,0)
		--load purchased items
		task.wait(.5)
		local things = plr.PurchasedFolder:GetChildren()
		print(things)
		if things ~= nil then
			for i,v in things do
				local obj
				repeat
					obj = SerStor.tycoons[tycoon.Name].TYCOON_OBJECTS:FindFirstChild(v.Name)
					task.wait()
				until obj ~= nil
				local clone = obj:Clone()
				clone.Parent = objects
				print(clone.Name..' has been added')
				task.wait(.01)
			end
		end
		for i,v in pairs(objects:GetChildren()) do
			addIncome(v)
		end
		updateButtons()
		
	end
end

tycoon.Owner.Changed:Connect(function(plr)
	OwnerCheck(plr)
end)

obj = SerStor.tycoons[tycoon.Name].TYCOON_OBJECTS:FindFirstChild(v.Name)
this line is probably always nil so it waits forever

you can replace it with :WaitForChild which is better for performance and also tells you when theres infinite waiting

it’s not nil, every instance exists, and i tried. yet it says it’s nil if i remove the error protection

did you put a print inside of the repeat to see if it keeps going forever

I did. it keeps going forever, but the instance does exist

then the name doesnt match (might be invisible character or space) or the instance is in the wrong place

Data wipe somehow helped and i fixed 1 mistake. this save was ok before all this mess. sometimes weird things happen

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