Tycoon buy functions not working

So i’ve recently started making a tycoon game. I am not that knowledgeable about scripting, so I followed a tutorial I found so i can learn how to script a simple tycoon. It all went fine until it came to the buying functions or buy buttons. I wrote it exactly how the tutorial wrote it, but instead of working, it broke the whole tycoon by breaking the collector part, it still destroyed things but it didn’t count the value anymore, so the player couldn’t earn anything.

So in summary: Script without buying functions worked fine (The parts were getting destroyed and the player could earn money) But if the buying functions were added then the tycoon would break and the buying functions wouldn’t work.

Here’s the MainScript with the buying functions (under the name “Buying Functions”)

--- Variables ---

local TycoonModel = script.Parent.Parent
local Items = {}

local BoughtItems = TycoonModel:FindFirstChild("BoughtItems")
local Buttons = TycoonModel:FindFirstChild("Buttons")
local DropperParts = TycoonModel:FindFirstChild("DropperParts")
local MainItems  = TycoonModel:FindFirstChild("MainItems")
local Scripts = TycoonModel:FindFirstChild("Scripts")
local Values = TycoonModel:FindFirstChild("Values")

--- Owner Functions ---

MainItems.OwnerDoor.MainDoor.Touched:Connect(function(Hit)
	if Hit.Parent:FindFirstChild("Humanoid")and Values.OwnerValue.Value == nil then
		local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
		Values.OwnerValue.Value = Player
		MainItems.OwnerDoor.MainDoor.SurfaceGui.TextLabel.Text = tostring(Values.OwnerValue.Value).."'s Factory"
	end
end)


--- Buying Functions ---

for i, v in pairs(Buttons:GetChildren()) do
	local NewItem = BoughtItems:FindFirstChild(v.Item.Value)
	if NewItem ~= nil then
		Items[NewItem.Name] = NewItem:Clone()
		NewItem:Destroy()
	else 
		v.BuyPart.Transparency = 1
		v.BuyPart.CanCollide = false
	end
	
	if v:FindFirstChild("Dependency") then
		coroutine.resume(coroutine.create(function()
			v.BuyPart.Transparency = 1
			v.BuyPart.CanCollide = false
			if BoughtItems:WaitForChild(v.Dependency.Value, 100000) then
				v.BuyPart.Transparency = 0
				v.BuyPart.CanCollide = true
			end
		end))
	end
	
	v.BuyPart.Touched:Connect(function(Hit)
		if Hit.Parent:FindFirstChild("Humanoid") then
			local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
			if Values.OwnerValue.Value == Player then
				if v.BuyPart.CanCollide == true and v.BuyPart.Transparency == 0 then
					if Player:WaitForChild("leaderstats").Bucks.Value >= v.Price.Value then
						Player.leaderstats.Bucks.Value -= v.Price.Value
						Items[v.Item.Value].Parent = BoughtItems
						v:Destroy()
					end
				end
			end
		end
	end)
end



--- Bucks Functions ---

local Debounce = false

MainItems.BucksButton.ButtonPart.Touched:Connect(function(Hit)
	if Hit.Parent:FindFirstChild("Humanoid") then
		local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
		if Values.OwnerValue.Value == Player then
			if Debounce == false then
				Debounce = true
				Player:WaitForChild("leaderstats").Bucks.Value += Values.BucksValue.Value
				wait()
				Values.BucksValue.Value = 0
				wait(0.5)
				Debounce = false
			end
		end
	end
end)

while wait() do
	MainItems.BucksButton.ScreenPart.SurfaceGui.TextLabel.Text = Values.BucksValue.Value
end

What it was intended to do:
Checks if the player has enough “Bucks” to buy a certain item. If yes then it would destroy the button and make the bought item (In this case the first dropper) appear, also deducting the value the bought item was worth from the player their “Bucks”. If no, then it the button will not get destroyed and the bought item will not appear, it also won’t deduct any “Bucks” from the player.

What it did:
It broke the tycoon by not registering any value inside the dropped items that were destroyed and thus not allowing the player to earn any bucks. It also doesn’t do it’s intended purpose by allowing things to be bought.


The folder “Buttons” is where the buttons are where you stand on to buy stuff.
The folder “MainItems” are the things you start with (conveyer, door, etc.)
The folder “BoughtItems” Are the things that can be bought.

This is how it looks while playing. The dropper should be invisible and the yellow button should be working.

If you have any idea on how to fix this or if you need any more information, please let me know and i’ll do whatever i can.

Thank you in advance.

(Credits to Gamer M8 for the script)

1 Like