MouseButton1Click doubling values when fired

Hello devs.
I created a script that when the player clicks on a proximity prompt it gives him two options to a car, cancel or buy.

But for some reason every time I click on buy it doubles the values, as you can see in the video below:

Server Script:

AreYouSureRE.OnServerEvent:Connect(function(player,carName,price)
	if player.Money.Value >= price then player.Money.Value -= price
		local CarValue = Instance.new("StringValue"); CarValue.Name = carName; CarValue.Parent = player.PlayerCarsInfoFolder.Cars
	end
end)

Client:

PPService.PromptTriggered:Connect(function(proximity, player)
	if proximity.Name == "BuyCar" then
		Main.Visible = true
		AreYouSureLabel.Text = "Are You sure You wan't to Buy " .. proximity.Parent.Parent.Name .. "?"
		
		Buy.MouseButton1Click:Connect(function()
			Main.Visible = false
			AreYouSureRE:FireServer(proximity.Parent.Parent.Name, proximity.Parent.Parent.Price.Value)
		end)

		Cancel.MouseButton1Click:Connect(function()
			Main.Visible = false
		end)
	end
end)

I’ve tried everything but it still doesn’t work, what do you guys suggest?

Try adding a debounce:

local debounce = false
AreYouSureRE.OnServerEvent:Connect(function(player,carName,price)
	if player.Money.Value >= price and debounce == false then 
		player.Money.Value -= price
		local CarValue = Instance.new("StringValue"); CarValue.Name = carName; CarValue.Parent = player.PlayerCarsInfoFolder.Cars
		debounce = true
		wait(0.5)
		debounce = false
	end
end)

Even with debounce didn’t work
made me think, does the problem starting on the client?

Did you try disconnecting the mouseclick event?

1 Like

For some reason your client must be double-firing the action. Try placing the debounce on the client instead.

I just realized something… the debounce was inside the event, it worked now on the outside!

Wait, I figured it out… (code will show upon edit)
oh, nevermind

but anyway thank you very much :joy:

Nevertheless, you can still make this code standalone:

Buy.MouseButton1Click:Connect(function()
			Main.Visible = false
			AreYouSureRE:FireServer(proximity.Parent.Parent.Name, proximity.Parent.Parent.Price.Value)
		end)

		Cancel.MouseButton1Click:Connect(function()
			Main.Visible = false
		end)
1 Like

Do you think putting a debounce is the best solution for this kind of problem?

But, make sure to add statements to check if it’s gonna work, too!

1 Like

Thinking about it, there’s probably a better solution. But for now, I’d just say that a debounce will work for now.

1 Like

Yes, maybe i can find a better solution using ā€œMousebutton1Clickā€ and ā€œProximity Promptsā€ in the same script. But anyway thank you very much

Everytime the proximity prompt is clicked, it adds another mouseclicked event, your debounce just stopped the second fire. Way to check this is to put a print in the mouse click, see on the second time if you buy it if it prints twice

This makes sense, the solution for this is to leave the buy and cancel options outside the if?

image

image

Let me know if this works!

local MouseEvent 
PPService.PromptTriggered:Connect(function(proximity, player)
	if proximity.Name == "BuyCar" then
		Main.Visible = true
		AreYouSureLabel.Text = "Are You sure You wan't to Buy " .. proximity.Parent.Parent.Name .. "?"
		
		MouseEvent = Buy.MouseButton1Click:Connect(function()
            MouseEvent:Disconnect()
			Main.Visible = false
			AreYouSureRE:FireServer(proximity.Parent.Parent.Name, proximity.Parent.Parent.Price.Value)
		end)

		Cancel.MouseButton1Click:Connect(function()
			Main.Visible = false
		end)
	end
end)
1 Like

Wow, wow and wow! This is actually the solution.

image

image

as expected, firing only once
I really appreciate!

1 Like

You were stacking event connections without appropriately disconnecting the previous connection (whenever the prompt’s ā€˜PromptTriggered’ signal fired).

It is important to remember that connections stack (they do not override).

local Game = game
local Players = game:GetService("Players")

local function OnPlayerRemoving(Player)
	print(Player.Name) --Prints the leaving player's name 100 times.
end

for _ = 1, 100 do
	Players.PlayerRemoving:Connect(OnPlayerRemoving) --Connection is created 100 times ('OnPlayerRemoving' callback function is connected to the player's 'PlayerRemoving' signal 100 times).
end
1 Like