Error With Button Script

That is odd, your original error was saying it wasn’t a child of ServerButtons

local tycoon = script.Parent.Parent

local mainItems = tycoon:FindFirstChild("MainItems")
local values = tycoon:FindFirstChild("Values")

local buttons = tycoon:FindFirstChild("Buttons")
local purchasedItems = tycoon:FindFirstChild("PurchasedItems")

local objects = {}

mainItems.OwnerDoor.Door.Touched:Connect(function(hit)
	if values.OwnerValue.Value == nil then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if player then
			if player:FindFirstChild("hasTycoon").Value == false then
				values.OwnerValue.Value = player
				mainItems.OwnerDoor.Door.SurfaceGui.ClaimText.Text = tostring(values.OwnerValue.Value).."'s Tycoon"
			end
		end
	end
end)

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
					v:Destroy()
				end

				if v:FindFirstChild("Dependency") then
					v.Button.Transparency = 1
					v.Button.CanCollide = false
					coroutine.resume(coroutine.create(function()
						if purchasedItems:WaitForChild(v.Dependency.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 values.OwnerValue.Value == player then
							if v.Button.CanCollide == true then
								if player:FindFirstChild("leaderstats").Cash.Value >= v.Price.Value then
									player.leaderstats.Cash.Value -= v.Price.Value
									objects[v.Object.Value].Parent = purchasedItems
									v:Destroy()
								end
							end
						end
					end
				end)
			end
		end)
	end
end

What error do you get when you try this?

Button is not a valid member of Model “ServerButton”

what line and before you leave the game send a screenshot of the server buttons

Line 48 and

image_2023-09-05_190835520

i meant a screenshot of server buttons and it’s children while the error occurs like in workspace

image_2023-09-05_191023426

I am very confused tbh, the error doesn’t make sense to me. from what I can tell your code is fine and the error is just weird. are you sure it’s not being destroyed or anything?

I think it destroys cause it thinks there is supposed to be a dependency and there is supposed to be one in future buttons.

that is probably the issue, try removing the code or just commenting out the destroy bit and then try it

That was the problem. But the button does not work.

try printing after the touched event to see if it detects the touch, add prints after each statement to see where the code breaks

Before I touched it I got this Inf Yeild: Infinite yield possible on 'Workspace.Tycoons.Tycoon.PurchasedItems:WaitForChild(“Server1”)

From a earlier screenshot you sent there is nothing inside the folder “PurchasedItems” so that’s why it probably yielded

Yes but it does this objects[v.Object.Value].Parent = purchasedItems

then I don’t think that is working

Should I do v.Parent = purchasedItems

EDIT: I realised it won’t work

1 Like

That I do not know since I don’t know what

is

nvm…

asjdasjlkdjaslkdjasiodu aiwodhjauiwoyhd

1 Like

I know it’s something with the destroy.

1 Like

Could you explain what “Dependency” is? I also tidied up your code and got rid of a part that may have caused the error.

Code:

local Players = game:GetService("Players")

local tycoon = script.Parent.Parent

local mainItems = tycoon.MainItems
local values = tycoon.Values

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

mainItems.OwnerDoor.Door.Touched:Connect(function(hit)
	if values.OwnerValue.Value then
		return
	end
	
	local player = Players:GetPlayerFromCharacter(hit.Parent)
	local hasTycoon = player and player:FindFirstChild("hasTycoon")
	
	if hasTycoon and not hasTycoon.Value then
		values.OwnerValue.Value = player
		mainItems.OwnerDoor.Door.SurfaceGui.ClaimText.Text = player.Name .."'s Tycoon"
	end
end)

for i, v in buttons:GetChildren() do
	task.spawn(function()
		if not v:FindFirstChild("Button") then
			return
		end

		local newObject = purchasedItems:FindFirstChild(v.Object.Value)
		local clonedObject = nil
		
		if newObject then
			clonedObject = newObject:Clone()
			newObject:Destroy()
		else
			v:Destroy()
			
			return
		end

		v.Button.Touched:Connect(function(hit)
			local player = Players:GetPlayerFromCharacter(hit.Parent)
			local leaderstats = player and player:FindFirstChild("leaderstats")
			
			if leaderstats and leaderstats.Cash.Value >= v.Price.Value and values.OwnerValue.Value == player and v.Button.CanCollide then
				leaderstats.Cash.Value -= v.Price.Value
				clonedObject.Parent = purchasedItems
				v:Destroy()
			end
		end)
	end)
end