Scripts ruining each other in tycoon...?

Hey! I’m currently working on a tycoon which is my upcoming project. I’m using Zed’s Tycoon Kit. This post might be a little confusing as I don’t know how to put it together.

I’ve made a GUI currency shop in order to allow the players buy currency for robux in-case they wanted to.

Here are pictures of the shop, the local script that prompts the purchase of the devproduct when a currency button is clicked, and the script in ServerScriptService that gives the player the currency when he purchases a devproduct


image

Some times, the shop works, some times, it does not. I’ve tried figuring out the problem and I’ve found out that a button in the tycoon is ruining the script. The button requires you to buy a devproduct before you can buy the “Manager” (Basically a upgrader).

These are pictures of the button and the part of the PurchaseHandler script which handles the devproduct buttons. "v" in the script is a variable for the button.

image

When the currency shop works, the button does not work. When the currency shop does not work, the button works.

I’ve tried hiring 2 scripters and both of them were unable to find the problem. I even gave them access to my game yet they still couldn’t fix it.

I’m still a beginner, so I can’t really figure out the problem, which is why I chose to post this.

Thank you :slight_smile:

Full PurchaseHandler script:

local Objects = {}
local TeamColor = script.Parent.TeamColor.Value
local Settings = require(script.Parent.Parent.Parent.Settings)
local Money = script.Parent.CurrencyToCollect
local Debris = game:GetService('Debris')
local Stealing = Settings.StealSettings
local CanSteal = false -- don't change or else you won't be able to steal currency
local MarketPlaceService = game:GetService("MarketplaceService")

script.Parent.Essentials.Spawn.TeamColor = TeamColor
script.Parent.Essentials.Spawn.BrickColor = TeamColor

function Sound(part,id)
	if part:FindFirstChild('Sound') then
		return
	else
		local Sound = Instance.new('Sound',part)
		Sound.SoundId = "rbxassetid://"..tostring(id)
		Sound:Play()
		delay(Sound.TimeLength, function()
			Sound:Destroy()
		end)
	end
end

--Parts that fall into the collector(s) get processed
for i,v in pairs(script.Parent.Essentials:GetChildren()) do
	if v.Name == "PartCollector" then
		v.Touched:connect(function(Part)
			if Part:FindFirstChild('Cash') then
				Money.Value = Money.Value + Part.Cash.Value
				Debris:AddItem(Part,0.1)
			end
		end)
	end
end

--Player Touched Collector processor
deb = false
script.Parent.Essentials.Giver.Touched:connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player ~= nil then
		if script.Parent.Owner.Value == player then
			if hit.Parent:FindFirstChild("Humanoid") then
				if hit.Parent.Humanoid.Health > 0 then
					if deb == false then
						deb = true
						script.Parent.Essentials.Giver.BrickColor = BrickColor.new("Medium stone grey")
						local Stats = game.ServerStorage.PlayerMoney:FindFirstChild(player.Name)
						if Stats ~= nil then 
						Sound(script.Parent.Essentials, Settings.Sounds.Collect)
						Stats.Value = Stats.Value + Money.Value
						Money.Value = 0
						wait(1)
						script.Parent.Essentials.Giver.BrickColor = BrickColor.new("Lily white")
						deb = false
						end
					end
				end
			end
		elseif Stealing.Stealing then -- if player isn't owner and stealing is on
			if CanSteal == true then
				CanSteal = false
				delay(Stealing.PlayerProtection, function()
					CanSteal = true
				end)
				if hit.Parent:FindFirstChild("Humanoid") then
					if hit.Parent.Humanoid.Health > 0 then
						local Stats = game.ServerStorage.PlayerMoney:FindFirstChild(player.Name)
						if Stats ~= nil then
							local Difference = math.floor(Money.Value * Stealing.StealPrecent)
							Sound(script.Parent.Essentials, Settings.Sounds.Collect)
							Stats.Value = Stats.Value + Difference
							Money.Value = Money.Value - Difference
						end
					end
				end
			else
				Sound(script.Parent.Essentials, Settings.Sounds.Error)
			end
		end
	end
end)

script.Parent:WaitForChild("Buttons")
for i,v in pairs(script.Parent.Buttons:GetChildren()) do
	spawn(function()
	if v:FindFirstChild("Head") then
		
		local ThingMade = script.Parent.Purchases:WaitForChild(v.Object.Value)
		if ThingMade ~= nil then
			Objects[ThingMade.Name] = ThingMade:Clone()
			ThingMade:Destroy()
		else
			--//Button doesn't have object, remove it
			error('Object missing for button: '..v.Name..', button has been removed')
			v.Head.CanCollide = false
			v.Head.Transparency = 1
		end
								
		if v:FindFirstChild("Dependency") then --// if button needs something unlocked before it pops up
			v.Head.CanCollide = false
			v.Head.Transparency = 1
			coroutine.resume(coroutine.create(function()
				if script.Parent.PurchasedObjects:WaitForChild(v.Dependency.Value) then
					if Settings['ButtonsFadeIn'] then
						for i=1,20 do
							wait(Settings['FadeInTime']/20)
							v.Head.Transparency = v.Head.Transparency - 0.05
						end
					end
					v.Head.CanCollide = true
					v.Head.Transparency = 0
				end
			end))
		end
		
		v.Head.Touched:connect(function(hit)
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			if v.Head.CanCollide == true then
				if player ~= nil then
					if script.Parent.Owner.Value == player then
						if hit.Parent:FindFirstChild("Humanoid") then
							if hit.Parent.Humanoid.Health > 0 then
								local PlayerStats = game.ServerStorage.PlayerMoney:FindFirstChild(player.Name)
								if PlayerStats ~= nil then
									if (v:FindFirstChild('Gamepass')) and (v.Gamepass.Value >= 1) then
										if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId,v.Gamepass.Value) then
											Purchase({[1] = v.Price.Value,[2] = v,[3] = PlayerStats})
										else
											game:GetService('MarketplaceService'):PromptGamePassPurchase(player,v.Gamepass.Value)
										end
									elseif (v:FindFirstChild('DevProduct')) and (v.DevProduct.Value >= 1) then
										game:GetService('MarketplaceService'):PromptProductPurchase(player,v.DevProduct.Value)
									elseif PlayerStats.Value >= v.Price.Value then
										print("Success")
										Purchase({[1] = v.Price.Value,[2] = v,[3] = PlayerStats})
										Sound(v, Settings.Sounds.Purchase)
										
									else
										Sound(v, Settings.Sounds.ErrorBuy)
										
										-- Prompt for purchase
										if v.Price.Value > game:GetService("ServerStorage"):WaitForChild("PlayerMoney"):WaitForChild(player.Name).Value then
											MarketPlaceService:PromptProductPurchase(player, 1101592819)
										end
					
									end
								end
							end
						end
					end
				end
			end
		end)
		end
	end)
end

function Purchase(tbl)
	local cost = tbl[1]
	local item = tbl[2]
	local stats = tbl[3]
	stats.Value = stats.Value - cost
	Objects[item.Object.Value].Parent = script.Parent.PurchasedObjects
	if Settings['ButtonsFadeOut'] then
		item.Head.CanCollide = false
		coroutine.resume(coroutine.create(function()
			for i=1,20 do
				wait(Settings['FadeOutTime']/20)
				item.Head.Transparency = item.Head.Transparency + 0.05
			end
		end))
	else
		item.Head.CanCollide = false
		item.Head.Transparency = 1
	end
end

function Create(tab)
	local x = Instance.new('Model')
	Instance.new('NumberValue',x).Value = tab[1]
	x.Value.Name = "Cost"
	Instance.new('ObjectValue',x).Value = tab[2]
	x.Value.Name = "Button"
	local Obj = Instance.new('ObjectValue',x)
	Obj.Name = "Stats"
	Obj.Value = tab[3]
	x.Parent = script.Parent.BuyObject
end

--// This was very rushed and is inefficent; if you plan on making something like this don't use a child added listener.
script.Parent:WaitForChild('BuyObject').ChildAdded:connect(function(child)
	local tab = {}
	tab[1] = child.Cost.Value
	tab[2] = child.Button.Value
	tab[3] = child.Stats.Value
	Purchase(tab)
	wait(10)
	child:Destroy()
end)

Full shop script:

mps.ProcessReceipt = function (receiptInfo)
	if receiptInfo.ProductId == 1097698253 then
		local plr = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)	
		wait (1)
		game:GetService("ServerStorage"):WaitForChild("PlayerMoney"):WaitForChild(plr.Name).Value = game:GetService("ServerStorage"):WaitForChild("PlayerMoney"):WaitForChild(plr.Name).Value + 2500
		print ("Money received")
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
	
	if receiptInfo.ProductId == 1097913182 then
		local plr = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
		wait (1)
		game:GetService("ServerStorage"):WaitForChild("PlayerMoney"):WaitForChild(plr.Name).Value = game:GetService("ServerStorage"):WaitForChild("PlayerMoney"):WaitForChild(plr.Name).Value + 5000
		print ("Money received")
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
	
	if receiptInfo.ProductId == 1097913588 then
		local plr = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
		wait (1)
		game:GetService("ServerStorage"):WaitForChild("PlayerMoney"):WaitForChild(plr.Name).Value = game:GetService("ServerStorage"):WaitForChild("PlayerMoney"):WaitForChild(plr.Name).Value + 10000
		print ("Money received")
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
	
	if receiptInfo.ProductId == 1097913779 then
		local plr = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
		wait (1)
		game:GetService("ServerStorage"):WaitForChild("PlayerMoney"):WaitForChild(plr.Name).Value = game:GetService("ServerStorage"):WaitForChild("PlayerMoney"):WaitForChild(plr.Name).Value + 25000
		print ("Money received")
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
	
	if receiptInfo.ProductId == 1097913780 then
		local plr = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
		wait (1)
		game:GetService("ServerStorage"):WaitForChild("PlayerMoney"):WaitForChild(plr.Name).Value = game:GetService("ServerStorage"):WaitForChild("PlayerMoney"):WaitForChild(plr.Name).Value + 50000
		print ("Money received")
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
	
	if receiptInfo.ProductId == 1101584970 then
		local plr = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
		wait (1)
		game:GetService("ServerStorage"):WaitForChild("PlayerMoney"):WaitForChild(plr.Name).Value = game:GetService("ServerStorage"):WaitForChild("PlayerMoney"):WaitForChild(plr.Name).Value + 100000
		print ("Money received")
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
	
	if receiptInfo.ProductId == 1101592819 then
		local plr = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
		wait (1)
		game:GetService("ServerStorage"):WaitForChild("PlayerMoney"):WaitForChild(plr.Name).Value = game:GetService("ServerStorage"):WaitForChild("PlayerMoney"):WaitForChild(plr.Name).Value + 250000
		print ("Money received")
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
	
end
1 Like

By “Button”, I mean the button which you can step on to buy the “Manager”. By “Currency button”, I mean the buttons in the shop GUI which you can click to prompt the purchase. The prompt purchase always works, but it doesn’t always give the cash you bought. When the “Button” works, the “Currency button” does not give you the cash you bought. When the “Button” does not work, the “Currency button” gives you the cash you bought. My goal here is to have both of them working. I’ve tried everything that could come to my mind but I still can’t seem to fix it.

1 Like

Could you provide your scripts? It is a bit difficult to help you determine the issue without those.

1 Like

Give me a second and I’ll post the full script.

1 Like

In addition to what @7z99 sayed, if you really don’t want to show your script for some reason, try to search IN the scripts if you, at some point, call for a variable, which would say for example that your gui = false, which can be good, since you don’t want that your gui is always showing up, but it can mess up if in another one you say false or other things. However, it’s hard to explain without the script, so the script would help us a lot!

1 Like

I’ve edited my post and added the scripts. There’s also a script in the Tycoon Kit that is called DevProductHandler and I’m not sure if it is the one doing this. Here’s the script of the DevProductHandler.

local Settings = require(script.Parent.Settings)
local Tycoon = script.Parent.Tycoons:GetChildren()[1]
script.Parent = game.ServerScriptService
local DevProducts = {}
local MarketplaceService = game:GetService('MarketplaceService')

for i,v in pairs(Tycoon:WaitForChild('Buttons'):GetChildren()) do
	if v:FindFirstChild('DevProduct') then
		if v.DevProduct.Value > 0 then
			DevProducts[v.DevProduct.Value] = v -- the button
		end
	end
end

MarketplaceService.ProcessReceipt = function(receiptInfo)
	for i,plr in pairs(game.Players:GetPlayers()) do
		if plr.userId == receiptInfo.PlayerId then
			if DevProducts[receiptInfo.ProductId] then
				local obj = DevProducts[receiptInfo.ProductId]
				local PlrT = game.ServerStorage.PlayerMoney:WaitForChild(plr.Name).OwnsTycoon
				if PlrT.Value ~= nil then
					--if PlrT.Value.PurchasedObjects:FindFirstChild(obj.Object.Value) == false then
						local PlayerStats = game.ServerStorage.PlayerMoney:FindFirstChild(plr.Name)
						Create({[1] = 0,[2] = obj,[3] = PlayerStats}, PlrT.Value.BuyObject)
					--end
				end
			end
		end
	end
end

function Create(tab, prnt)
	local x = Instance.new('Model')
	Instance.new('NumberValue',x).Value = tab[1]
	x.Value.Name = "Cost"
	Instance.new('ObjectValue',x).Value = tab[2]
	x.Value.Name = "Button"
	local Obj = Instance.new('ObjectValue',x)
	Obj.Name = "Stats"
	Obj.Value = tab[3]
	x.Parent = prnt
end
1 Like

I am not sure if it’s just me but I feel like this script is quite difficult to read, also considering :connect() is deprecated, the script may be dated. Perhaps you’d be interested in switching over to using . PromptPurchaseFinished? I could try to help you with that. If you don’t want to, ensure you are only using a single script, as the developer.roblox.com website states.

1 Like

Well, I’m using a kit and I’m not a scripter so changing stuff by my own will only make the matter worse. It’d be great if you helped me do what you said :slight_smile:

1 Like

Alrighty, well this is basically where to start. I’m going to get the model you used right now to verify. Just to confirm, you haven’t changed any hierarchy of any objects inside of the model, correct? Also, all of the tycoons are parented to the Tycoons folder?

local marketPlaceService = game:GetService('MarketplaceService')

marketPlaceService.PromptPurchaseFinished:Connect(function(player, assetID, isPurchased)
	if isPurchased then
    	print(player.Name..' has purchased the product with the ID '..assetID)
    end
end)
1 Like

I have not changed the hierarchy of any objects inside of the model, and yes, all the tycoon are parented to the Tycoons folder. Also, where do I put that script?

1 Like

You can put that script anywhere, but probably ServerScriptService would be the best. Perhaps we should switch over to PMs so we don’t spam the forum.