Button destroyed

No errors, but when I test the game, the second button which should appear when I pressed the first button is destroyed as I started the game. The destroy action was supposed to only destroy the droppers inside PurchasedItems.

local tycoon = script.Parent

local buttons = tycoon.Buttons
local purchasedItems = tycoon.PurchasedItems

local owner = tycoon.Owner

local objects = {}

if buttons then
	for i, v in pairs(buttons:GetChildren()) do
		spawn(function()
			if v:FindFirstChild("Button") then
				
				local newObject = purchasedItems:FindFirstChild(v.Object.Value)
				if newObject ~= nil then
					objects[newObject.Name] = newObject:Clone()
					newObject:Destroy()
				else
					newObject:Destroy()
				end
				
				if v:FindFirstChild("Depedency") then
					v.Button.Transparency = 1
					v.Button.CanCollide = false
					coroutine.resume(coroutine.create(function()
						if purchasedItems:WaitForChild(v.Depedency.Value) then
							v.Button.Transparency = 0
							v.Button.CanCollide = true
						end
					end))
				end
				
				v.Button.Touched:Connect(function(hit)
					local player = game.Players:GetPlayerFromCharacter(hit.Parent)
					if player then
						if owner.Value == player then
							if v.Button.CanCollide == true then
								if player:FindFirstChild("leaderstats").Money.Value >= v.Price.Value then
									player.leaderstats.Money.Value -= v.Price.Value
									objects[v.Object.Value].Parent = purchasedItems
									v:Destroy()
								end
							end
						end
					end
				end)
			end
		end)
	end
end

Try this:

local tycoon = script.Parent

local buttons = tycoon:WaitForChild("Buttons")
local purchasedItems = tycoon:WaitForChild("PurchasedItems")
local owner = tycoon:WaitForChild("Owner")

local objects = {}

for _, buttonModel in pairs(buttons:GetChildren()) do
	task.spawn(function()
		local button = buttonModel:FindFirstChild("Button")
		local objectValue = buttonModel:FindFirstChild("Object")
		local priceValue = buttonModel:FindFirstChild("Price")
		local dependencyValue = buttonModel:FindFirstChild("Depedency")

		if button and objectValue and priceValue then
			local objectName = objectValue.Value
			local item = purchasedItems:FindFirstChild(objectName)

			if item and item.Name:lower():find("dropper") then
				objects[objectName] = item:Clone()
				item:Destroy()
			end

			if dependencyValue then
				button.Transparency = 1
				button.CanCollide = false

				task.spawn(function()
					purchasedItems:WaitForChild(dependencyValue.Value)
					button.Transparency = 0
					button.CanCollide = true
				end)
			end

			button.Touched:Connect(function(hit)
				local player = game.Players:GetPlayerFromCharacter(hit.Parent)
				if player and owner.Value == player then
					local stats = player:FindFirstChild("leaderstats")
					if stats and stats:FindFirstChild("Money") then
						if button.CanCollide and stats.Money.Value >= priceValue.Value then
							stats.Money.Value -= priceValue.Value

							local newItem = objects[objectName]
							if newItem then
								newItem.Parent = purchasedItems
							end

							buttonModel:Destroy()
						end
					end
				end
			end)
		end
	end)
end

this works but now the droppers and the second button appeared as I enter the game

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