Function keeps running twice

so im trying to make a donut shop and it works (stock, price, the hunger it gives, the gui, etc)
ON THE FIRST time, but then it breaks

close.MouseButton1Click:Connect(function()
		local price = 0
		local buyfunction = false
		local nobuyfunction = false

		for i,v in ipairs(boughtdonuts) do
			local founddonut = desc.Parent:FindFirstChild(v)
			
			if founddonut then
				price = price + founddonut.Stats.Price.Value
			end
			
			
			
		end
		
		local money = game.Players.LocalPlayer.leaderstats.Money.Value
		if money >= price then
print()
				local function CutsceneBuyDonuts( cam1)
					print('ran')
					dialogue.Parent.DonutShop.Enabled = false
					game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 0
					game.Players.LocalPlayer.Character.Humanoid.JumpHeight = 0
					game.Workspace.Camera.CameraType = Enum.CameraType.Scriptable
					game.Workspace.Camera.CameraSubject = cam1
					game.Workspace.Camera.CFrame = cam1.CFrame
					prox.Enabled = false
					dialogue.Enabled = true
					dialogue.PersonName.Text = 'Jim'
					dialogue.Text.Text = 'You bought..'
					wait(2)
					print(boughtdonuts)
					while true do
					if #boughtdonuts > 0 then

						for i,donut in ipairs(boughtdonuts) do
							dialogue.Text.Text = '1 ' .. donut .. ','
							wait(2)
							table.remove(boughtdonuts, i)
						end

					elseif #boughtdonuts == 0 and #keepdonuts == 0 then

						dialogue.Text.Text = '...'
						wait(2)
						dialogue.Text.Text = 'literally nothing :('

						wait(2)

					else
						dialogue.Text.Text = 'adding up to ' .. price .. ' dollars.'
						wait(2)
						dialogue.Text.Text = "Thank for purchasing at my shop"
						wait(2)
						dialogue.Text.Text = "Each purchase makes me just a tiny bit happier."
						wait(2)
						break
					end
					end
					



					while true do
						if #keepdonuts > 0 then
							local donut = math.random(1,#keepdonuts)
							local donutname = keepdonuts[donut]
							local donutremote = game.ReplicatedStorage.remotes.donut
							donutremote:FireServer(donutname)
							table.remove(keepdonuts, donut)


						else
							desc.Image.Image = ""
							desc.DonutName.Text = ""
							desc.Stock.Text = ""
							desc.Hunger.Text = ""
							desc.Price.Text = ""
							desc.Buy.Text = ""
							break
						end
					end





					game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
					game.Players.LocalPlayer.Character.Humanoid.JumpHeight = 7.2
					game.Workspace.Camera.CameraType = Enum.CameraType.Custom
					game.Workspace.Camera.CameraSubject = game.Players.LocalPlayer.Character.Humanoid
					dialogue.Enabled = false
					prox.Enabled = true

					return
				end

				CutsceneBuyDonuts(cutscenekit.JimCam)
				return
	


		

		end

		if money < price then
		
			local function Cutscene( cam1)
				dialogue.Parent.DonutShop.Enabled = false
				game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 0
				game.Players.LocalPlayer.Character.Humanoid.JumpHeight = 0
				game.Workspace.Camera.CameraType = Enum.CameraType.Scriptable
				game.Workspace.Camera.CameraSubject = cam1
				game.Workspace.Camera.CFrame = cam1.CFrame
				prox.Enabled = false
				dialogue.Enabled = true
				dialogue.PersonName.Text = 'Jim'
				dialogue.Text.Text = 'Erm, it seems like you dont have enough money to buy those donuts.'
				wait(2)



				while true do
					if #boughtdonuts > 0 then

						local donut = math.random(1,#boughtdonuts)
						local thedonut = game.Players.LocalPlayer.PlayerGui.DonutShop.Main:FindFirstChild(boughtdonuts[donut])
						print(boughtdonuts[donut])
						thedonut.Stats.OnStock.Value = true


						table.remove(boughtdonuts, donut)
						table.remove(keepdonuts, donut)
					else
						wait(2)
						dialogue.Text.Text = "Your donuts have been restocked."

						wait(2)
						desc.Image.Image = ""
						desc.DonutName.Text = ""
						desc.Stock.Text = ""
						desc.Hunger.Text = ""
						desc.Price.Text = ""
						desc.Buy.Text = ""
						print(#boughtdonuts)
						break
					end
					wait()
				end


				game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
				game.Players.LocalPlayer.Character.Humanoid.JumpHeight = 7.2
				game.Workspace.Camera.CameraType = Enum.CameraType.Custom
				game.Workspace.Camera.CameraSubject = game.Players.LocalPlayer.Character.Humanoid
				dialogue.Enabled = false
				prox.Enabled = true
				return
			end
			
			if buyfunction == false then
				buyfunction = true
				Cutscene(cutscenekit.JimCam)
				buyfunction = false


			end
		end
	end)

for some reason, on the second time, it prints β€œran” twice
Even tho i get my donuts, the dialogue texts breaks
if you need any sort of more explaining, lmk
thank you for your help

2 Likes

nvm i just addded a debounce lol

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.