Basically, when the user clicks an image button, they are greeted with a gui asking them if they would like to purchase said product.

When they confirm, the remote event is fired.

The problem is, the remote event fires depending on how many times it was previously run. For Instance, if 2 Caravels and 1 Dinghy had been purchased previously, buying another Boat will run the function 4 times.

When the player resets, it goes back to 1, but increases once again depending on how many purchases had been previously made.

local Server = game:GetService("Players")
local Client = Server.LocalPlayer
local Workspace = game:GetService("Workspace")
local Replicated_Storage = game:GetService("ReplicatedStorage")
local Remote_Events = Replicated_Storage:WaitForChild("#$Remote_Events")
local Friendly_NPCs = Workspace:WaitForChild("Friendly_NPC's")
local Sailor = Friendly_NPCs:WaitForChild("Sailor")
local Part = Sailor:WaitForChild("HumanoidRootPart")
local GUI = script.Parent
local Mainframe = GUI:WaitForChild("MainUI")
local Scrollingframe = Mainframe:WaitForChild("ScrollingFrame")
local PurchaseUI = GUI:WaitForChild("PurchaseUI")
local Purchasing = false
local Confirm = PurchaseUI:WaitForChild("Confirm")
local Decline = PurchaseUI:WaitForChild("Decline")

local Boats = {
	Dinghy = {
		Price = 1500,
		Item = "Dinghy"
		
	},
	Caravel = {
		Price = 10000,
		Item = "Caravel"
	},
}


for looping, buttons in pairs(Scrollingframe:GetChildren()) do
	
	if buttons:IsA("ImageButton") then	
		buttons.MouseButton1Click:Connect(function(plr)
			
			if Purchasing == false then
				Purchasing = true
				PurchaseUI:WaitForChild("BoatName").Text = ""..Boats[buttons.Name].Item.." for "..Boats[buttons.Name].Price.." Berries?"
				Mainframe.Visible = false
				PurchaseUI.Visible = true
				
				Confirm.MouseButton1Click:Connect(function()
					
					Purchasing = false
					Mainframe.Visible = true
					PurchaseUI.Visible = false

					local PurchaseEvent = Remote_Events:WaitForChild("ConfirmPurchase")
					PurchaseEvent:FireServer(Boats[buttons.Name].Item)
					print("fired")
					
				end)
				
				Decline.MouseButton1Click:Connect(function()
					
					Purchasing = false
					PurchaseUI.Visible = false
		
				end)
				
			end	
		end)		
	end
end


while wait(1) do
	


	local HumanoidRootPart = Client.Character:WaitForChild("HumanoidRootPart", 10)
	if HumanoidRootPart then
		local Distance = (Part.Position - HumanoidRootPart.Position).Magnitude
		if Purchasing == false then
			if Distance <= 10 then
				Mainframe.Visible = true
			else
				Mainframe.Visible = false
			end
		else print("purchasing = true")
		end
	end
end




2 comments

0

You're nestling connections, each time buttons.MouseButton1Click fires you connect two additional listeners two Confirm and Decline. These will stack up after each click.

0

So in other words, it is because of the for loop?