Error With Button Script

Hello fellow developers! I have an error saying: Button is not a valid member of Model “ServerButton”, even though I have the right code. Code:

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)
4 Likes

Ok. Here is the code:

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
1 Like

No, it looks through all of the buttons in the buttons folder so it all works the same as the other buttons.

I am planning to add more buttons

Yes.

justfillinginspaceandbobloxmoderationisbad

1 Like

could you please send a pic of whats inside the modle

1 Like

image_2023-09-05_182906934

2 Likes

image
This part doesn’t make sense, you looped through the children of Server buttons, and V is the Button.
Use this instead.

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
				if v.Name == "Button" then
					v.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
end```

Let me know if that worked

I don’t get an error but it does not show.

I just realized a mistake I made, gimme a second to fix it

that won’t work because v.name is not “Button” it is the child that has the name “Button”

He loops through the Model “Server Buttons” which it is a child of

I am adding more buttons soon.

Nvm I miss read that you’re right i just saw the screenshot from earlier

try this

v.Button.Touched:Connect(function(hit)
if v:FindFirstChild(“Button”) then
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)

v is the model tho
can you check again

try this and tell me if this works

There is an error. I think you know what it is (;

image
Shouldn’t it be this?
Server Buttons is the child of the Buttons Folder not Button