Help with Tycoon main script; Bugfix

I’m sorry this code is a bit long, but here is my issue.
Whenever I run this code, one of two things happen:

  1. (With Wait)The code will run repeatedly, causing the script to take away more money from the player than it should

  2. (Without Wait) The code will run once, but delete the button instantly, causing no animations to play.

I do not know how else I would make it run ONLY once while still playing all of the animations, other than the debounce method, which doesn’t seem to be working.
Here is my script:

local DB = false
		touched = v.Button.Touched:Connect(
			function(Hit)
			print(v.Price.Value)
			if DB == false then 
			DB = true
				if v.Button.CanCollide == true then
					if Hit and Hit.Parent and Hit.Parent:FindFirstChild("Humanoid") then
						local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
						if Player and OwnerValue.Value == Player then
							if Player.leaderstats.Cash.Value >= v.Price.Value then
									Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value - v.Price.Value
								if bparts:IsA("BasePart") then
									local FadeOut = TweenS:Create(bparts, tweenFade, partPropsOut)
									FadeOut:Play()
									bparts.CanCollide = true
									if bparts.Name == "Button" then
										bparts.ButtonBuy:Play()
										bparts.PriceGUI.Enabled=true
										bparts.partem.Enabled = true
									end
								end		
								Objects[v.Object.Value].Parent = Purchases
								wait(1) -- here
								v:Destroy()
								db2 = false
							end
						end 
					end
				end
			DB = false
			end
		end)

This is not the whole script, only the piece I believe to have the issue. If you need the whole script, just tell me and I will send it. Sorry if I missed any information. Thank you in advance!

1 Like

I’m assuming that this is supposed to only run once, so there’s no reason to set DB back to false.
This should fix the issue of it running multiple times.

Thanks, I’ll try. It might not work just because it is a for loop; It needs to call it once for each button.

Didn’t work. I think the DB might not be actually checking. I just ran a print for DB and it did print, I think it somehow didn’t actually check for the DB.

No, it checks,

if DB == false then 

So just to be sure you removed this line:

DB = false

And kept the waits() in?

Yes, I did remove the line, but it still kept calling. I put in a print(DB) and it printed true 12 times. I just recorded a GIF. Give me a second and I’ll have one showing it take money too. The first value prints the price of the item, the second is the value of DB.


Sorry if its low res, but it prints the price 66 times and true 12 times.

Can you show just a bit more of your code where you iterate through each of the buttons.

Please also show your updated code with the DB removed and the wait()s, and the print(DB)

Here’s the whole code:

for i, v in pairs(Buttons:GetChildren()) do
	local Object = Purchases:FindFirstChild(v.Object.Value)
	if Object then
		Objects[Object.Name] = Object:Clone()
		Object:Destroy()
	end
	
	for _, bparts in ipairs(v:GetDescendants()) do
		--tween
		local tweenFade = TweenInfo.new(
			1,
			Enum.EasingStyle.Exponential,
			Enum.EasingDirection.Out,
			0,
			false,
			0
		)
		
		local partPropsOut = {
			Transparency = 1
		}
		
		local partPropsIn = {
			Transparency = 0
		}
		
		
		if v:FindFirstChild("Dependancy") then
			coroutine.resume(coroutine.create(function()
				
				
				if bparts:IsA("BasePart") then
					bparts.Transparency = 1
					bparts.CanCollide = false
					if bparts.Name == "Button" then
						bparts.PriceGUI.Enabled = false
						bparts.partem.Enabled = false
					end
				end
				if Purchases:WaitForChild(v.Dependancy.Value) then
					if bparts:IsA("BasePart") then
						local FadeIn = TweenS:Create(bparts, tweenFade, partPropsIn)
						FadeIn:Play()
						
						bparts.CanCollide = true
						
						if bparts.Name == "Button" then
							bparts.PriceGUI.Enabled=true
							bparts.partem.Enabled = true
						end
					end
				end
			end))
		end
		local DB = false
		touched = v.Button.Touched:Connect(
			function(Hit)
			print(v.Price.Value)
			if DB == false then 
				DB = true
				if v.Button.CanCollide == true then
					if Hit and Hit.Parent and Hit.Parent:FindFirstChild("Humanoid") then
						local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
						if Player and OwnerValue.Value == Player then
							if Player.leaderstats.Cash.Value >= v.Price.Value then
									Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value - v.Price.Value
								if bparts:IsA("BasePart") then
									local FadeOut = TweenS:Create(bparts, tweenFade, partPropsOut)
									FadeOut:Play()
									bparts.CanCollide = true
									if bparts.Name == "Button" then
										bparts.ButtonBuy:Play()
										bparts.PriceGUI.Enabled=true
										bparts.partem.Enabled = true
									end
								end		
								Objects[v.Object.Value].Parent = Purchases
								wait(1) -- here
								v:Destroy()
							end
						end 
					end
				end
				print(DB)
			--DB = false
			end
		end)
	end
end

Hmm, I’ve gotta go right now, I’ll be back in roughly 1 hour.

It has something to do with repeating the connection to part.Touched on for the same button 13 times.
The cost seems to get printed with an increment of 13 as well so it’s solidifying my suspicion.

For each of the objects you are making appear for the dropper, you are creating a new .Touched function on the button.

the function must be outside for loop

local DB = false
local function OnButtonTouch(Hit, v)
			print(v.Price.Value)
			if DB == false then 
				DB = true
				if v.Button.CanCollide == true then
					if Hit and Hit.Parent and Hit.Parent:FindFirstChild("Humanoid") then
						local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
						if Player and OwnerValue.Value == Player then
							if Player.leaderstats.Cash.Value >= v.Price.Value then
									Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value - v.Price.Value
								if bparts:IsA("BasePart") then
									local FadeOut = TweenS:Create(bparts, tweenFade, partPropsOut)
									FadeOut:Play()
									bparts.CanCollide = true
									if bparts.Name == "Button" then
										bparts.ButtonBuy:Play()
										bparts.PriceGUI.Enabled=true
										bparts.partem.Enabled = true
									end
								end		
								Objects[v.Object.Value].Parent = Purchases
								wait(1) -- here
								v:Destroy()
							end
						end 
					end
				end
				print(DB)
			--DB = false
			end
		end

for i, v in pairs(Buttons:GetChildren()) do
	local Object = Purchases:FindFirstChild(v.Object.Value)
	if Object then
		Objects[Object.Name] = Object:Clone()
		Object:Destroy()
	end
	
	for _, bparts in ipairs(v:GetDescendants()) do
		--tween
		local tweenFade = TweenInfo.new(
			1,
			Enum.EasingStyle.Exponential,
			Enum.EasingDirection.Out,
			0,
			false,
			0
		)
		
		local partPropsOut = {
			Transparency = 1
		}
		
		local partPropsIn = {
			Transparency = 0
		}
		
		
		if v:FindFirstChild("Dependancy") then
			coroutine.resume(coroutine.create(function()
				
				
				if bparts:IsA("BasePart") then
					bparts.Transparency = 1
					bparts.CanCollide = false
					if bparts.Name == "Button" then
						bparts.PriceGUI.Enabled = false
						bparts.partem.Enabled = false
					end
				end
				if Purchases:WaitForChild(v.Dependancy.Value) then
					if bparts:IsA("BasePart") then
						local FadeIn = TweenS:Create(bparts, tweenFade, partPropsIn)
						FadeIn:Play()
						
						bparts.CanCollide = true
						
						if bparts.Name == "Button" then
							bparts.PriceGUI.Enabled=true
							bparts.partem.Enabled = true
						end
					end
				end
			end))
		end
                v.Button.Touched:Connect(function() --there's the touch function!
                   OnButtonTouch(Hit, v)
               end)
	end
end



Where is the Touched function?

okay, now there is the Touched function

It is still calling more than once. I got the first button to work (While still calling more than once), but the second one won’t do anything.

did you get any errors in the output?

Actually, no. It only has called much more times than the original, but thats probably because the second button didnt work at all. Also its not the fact you forgot Hit in your function. I already noticed that.

try destroying the button after it’s touched, and make clone of it for fade out effect.

If I cloned it, It should do the same thing, since the main script finds all buttons. nvm hold on

you can create folder for these fading out buttons, and change the name of button so it doesn’t detect

Thanks for the Idea! I think it’s working now, all of the buttons are doing what I wanted them to. Seems like you fixed more of the issues I was having than one. Thanks man!

1 Like