Item keeps duplicating when bought until it stops working

I have a simple shop system that you can buy stuff with. However, it keeps duplicating when bought like 2 → 4 then 4 → 8 until it just stop working. Anyway care to explain what’s the problem and the solution?

local ShopKeeper = workspace.MilitaryBoi1232
local player = game:GetService("Players").LocalPlayer
local ProximityPrompt = ShopKeeper.Torso.ProximityPrompt
local ShopFrame = player.PlayerGui.ShopScreenGui.ShopFrameGui
local Cam = workspace.CurrentCamera
local CamPart = workspace.CamPart
local ComfirmationFrame = player.PlayerGui.ShopScreenGui.ComfirmationFrame
local ComfirmationText = ComfirmationFrame.ComfirmationText
local YesButton = ComfirmationFrame.YesButton
local NoButton = ComfirmationFrame.NoButton
local AppleButton = script.Parent.AppleImageButton
local MoneyValue = player:WaitForChild("TotalMoney")

ProximityPrompt.Triggered:Connect(function(plr)
	plr.Character.Humanoid.WalkSpeed = 0
	print("WalkSpeed Changed")
	Cam.CameraType = Enum.CameraType.Scriptable
	Cam.CFrame = CamPart.CFrame
	print("Cam Changed")
	ShopFrame:TweenPosition(UDim2.new(0.008,0,0.731,0),
		Enum.EasingDirection.InOut,
		Enum.EasingStyle.Linear,
		1,
		false)
	print("Tweened")
AppleButton.MouseButton1Click:Connect(function(plr)
		print('Buying Apple')
		local AppleCost = 5
		local Apple = game.ReplicatedStorage.Apple
		ComfirmationFrame.Visible = true
		ComfirmationText.Text = "Are you sure you want to buy this? It will cost you "..AppleCost.." Dollars"
		YesButton.MouseButton1Click:Connect(function()
			if MoneyValue.Value > 5 then
				print("More then "..AppleCost.." Dollars!")
				MoneyValue.Value = MoneyValue.Value - AppleCost
				print('Money Spent')
				print('Brought Apple')
				ComfirmationFrame.Visible = false
				local ClonedApple = Apple:Clone()
				ClonedApple.Parent = player.Backpack
			end
			NoButton.MouseButton1Click:Connect(function()
				print("nah")
				ComfirmationFrame.Visible = false
				return
			end)
		end)
	end)
end)

change this > to this < and see if that works…

It didn’t work, the same problem occured again

I messed up sorry, i meant to change it from this > to this >=

See if that works…

It still didn’t work, same problem occured
:upside_down_face:

Are you doing this in a local script? Can you invite me to the game? It will be a lot easier to fix the problem inside the game…

Yes I’m doing this in a local script, also I invited you

The game doesn’t pop up on Roblox studio, are you sure you invited me?

yes

The problem is being caused by the fact that you are connecting callbacks every time that the player triggers the prompt thus making the items duplicate.

You need to attach mouse clicks callbacks outside of the triggered callback

Since you do

every time that the player triggers the prompt, the first time there will be 1 callback attached giving 1 apple, the second time there will be 2 callbacks and so the player will receive two apples and so on

Also change > to >= so that if the player has 5 dollars they can buy the apple anyway and don’t need more

Also do not give random people permissions to edit your game, it’s not safe

I’m pretty new to scripting so how do I do that?

local ShopKeeper = workspace.MilitaryBoi1232
local player = game:GetService("Players").LocalPlayer
local ProximityPrompt = ShopKeeper.Torso.ProximityPrompt
local ShopFrame = player.PlayerGui.ShopScreenGui.ShopFrameGui
local Cam = workspace.CurrentCamera
local CamPart = workspace.CamPart
local ComfirmationFrame = player.PlayerGui.ShopScreenGui.ComfirmationFrame
local ComfirmationText = ComfirmationFrame.ComfirmationText
local YesButton = ComfirmationFrame.YesButton
local NoButton = ComfirmationFrame.NoButton
local AppleButton = script.Parent.AppleImageButton
local MoneyValue = player:WaitForChild("TotalMoney")

AppleButton.MouseButton1Click:Connect(function(plr)
    print('Buying Apple')
    local AppleCost = 5
    local Apple = game.ReplicatedStorage.Apple

    ComfirmationFrame.Visible = true
    ComfirmationText.Text = "Are you sure you want to buy this? It will cost you "..AppleCost.." Dollars"

    YesButton.MouseButton1Click:Connect(function()
        if MoneyValue.Value >= 5 then
	        print("More then "..AppleCost.." Dollars!")
	        MoneyValue.Value = MoneyValue.Value - AppleCost
		    print('Money Spent')
		    print('Brought Apple')
		    ComfirmationFrame.Visible = false

		    local ClonedApple = Apple:Clone()
		    ClonedApple.Parent = player.Backpack
		end
	end)
end)

NoButton.MouseButton1Click:Connect(function()
    print("nah")
	ComfirmationFrame.Visible = false
	return
end)

ProximityPrompt.Triggered:Connect(function(plr)
	plr.Character.Humanoid.WalkSpeed = 0
	print("WalkSpeed Changed")
	Cam.CameraType = Enum.CameraType.Scriptable
	Cam.CFrame = CamPart.CFrame
	print("Cam Changed")
	ShopFrame:TweenPosition(UDim2.new(0.008,0,0.731,0),
		Enum.EasingDirection.InOut,
		Enum.EasingStyle.Linear,
		1,
		false)
	print("Tweened")
end)

As you can see i moved the YesButton.MouseButton1Click:Connect(function() and NoButton.MouseButton1Click:Connect(function() outside of the prompt triggered callback.

I also changed > to >= so that if the player has exactly 5 dollars they can buy the apple anyway and they don’t need 6 or more

Idk what’s wrong, but it still didn’t work! :expressionless:

My bad i didn’t move the yes callback out of the apple button one

local ShopKeeper = workspace.MilitaryBoi1232
local player = game:GetService("Players").LocalPlayer
local ProximityPrompt = ShopKeeper.Torso.ProximityPrompt
local ShopFrame = player.PlayerGui.ShopScreenGui.ShopFrameGui
local Cam = workspace.CurrentCamera
local CamPart = workspace.CamPart
local ComfirmationFrame = player.PlayerGui.ShopScreenGui.ComfirmationFrame
local ComfirmationText = ComfirmationFrame.ComfirmationText
local YesButton = ComfirmationFrame.YesButton
local NoButton = ComfirmationFrame.NoButton
local AppleButton = script.Parent.AppleImageButton
local MoneyValue = player:WaitForChild("TotalMoney")

AppleButton.MouseButton1Click:Connect(function(plr)
    print('Buying Apple')
    local AppleCost = 5
    local Apple = game.ReplicatedStorage.Apple

    ComfirmationFrame.Visible = true
    ComfirmationText.Text = "Are you sure you want to buy this? It will cost you "..AppleCost.." Dollars"
end)

YesButton.MouseButton1Click:Connect(function()
        if MoneyValue.Value >= 5 then
	        print("More then "..AppleCost.." Dollars!")
	        MoneyValue.Value = MoneyValue.Value - AppleCost
		    print('Money Spent')
		    print('Brought Apple')
		    ComfirmationFrame.Visible = false

		    local ClonedApple = Apple:Clone()
		    ClonedApple.Parent = player.Backpack
		end
	end)

NoButton.MouseButton1Click:Connect(function()
    print("nah")
	ComfirmationFrame.Visible = false
	return
end)

ProximityPrompt.Triggered:Connect(function(plr)
	plr.Character.Humanoid.WalkSpeed = 0
	print("WalkSpeed Changed")
	Cam.CameraType = Enum.CameraType.Scriptable
	Cam.CFrame = CamPart.CFrame
	print("Cam Changed")
	ShopFrame:TweenPosition(UDim2.new(0.008,0,0.731,0),
		Enum.EasingDirection.InOut,
		Enum.EasingStyle.Linear,
		1,
		false)
	print("Tweened")
end)

This should work

1 Like

It finally worked, I had this problem for 2 days now, I do suck at scripting :rofl:

Remember that callbacks should be connected only once. You can use :Once instead of :Connect() if you want to connect them every time so that once they run they will automatically disconnect

2 Likes