My script does everything 5 times

So I’m making a shop system, and it prints 5 times. Everything else is removing 5 times as well, like when I buy 1 $20 dollar shirt, it removes $100 dollars from my leaderstats, and it gives me 5 skirts. Can you please help?

Script:

remotes.BuyStock.OnServerEvent:Connect(function(player, item, amount)
	if player and item and amount then
		print(player.Name.." is trying to buy "..amount.." "..item.Name)
		if stockItems:FindFirstChild(item.Name, true) then
			local leaderstats = player:WaitForChild("leaderstats")
			local itemsInStock = player:WaitForChild("ItemsInStock")
			
			local price = item:WaitForChild("Price")
			
			if leaderstats and itemsInStock then
				if leaderstats.Coins.Value >= (amount * price.Value) then
					leaderstats.Coins.Value -= (amount * price.Value)
					itemsInStock[item.Name].Value += amount
				end
			end
		end
	end
end)

Prints:

  15:15:09.351   ▶ catxkdb is trying to buy 1 Skirt (x5)  -  Server - Main:9
  15:15:14.584   ▶ catxkdb is trying to buy 2 Skirt (x5)  -  Server - Main:9
  15:15:16.117   ▶ catxkdb is trying to buy 3 Skirt (x5)  -  Server - Main:9
  15:15:17.250   ▶ catxkdb is trying to buy 4 Skirt (x5)  -  Server - Main:9

Add a debounce to prevent multiple firing events.

1 Like

I already have a debounce system in the client. should I put it in the server?

Sure, if you know how to implement a debounce table. If you don’t, just do it on the client.

Still doesn’t work with the debounce table。

I think it isn’t the debounce, it’s just that when I call the function, it somehow calls it 5 times.

How did you add the debounce anyway? Show me the new code.

It can’t be from the OnServerEvent it has to be from whatever fired it. Could I see the script that triggered it?

			local db = false
			if selectedTemp ~= nil then
				if db == false and db ~= true then
					db = true
					local item = game.ReplicatedStorage.StockItems[selectedTemp.Name]
					local result = game.ReplicatedStorage.Remotes.BuyStock:FireServer(item, amnt.Value)
					wait(.2)
					db = false
				end
			end
			local db = false
			if selectedTemp ~= nil then
				if db == false and db ~= true then
					db = true
					local item = game.ReplicatedStorage.StockItems[selectedTemp.Name]
					local result = game.ReplicatedStorage.Remotes.BuyStock:FireServer(item, amnt.Value)
					wait(.2)
					db = false
				end
			end

here

1 Like

Where is the script located? ㅤㅤㅤ

In a frame in ``StarterGui``` character limit

Where are you firing the event

In the client, in a LocalScript in the game’s StarterGui , in a ScreenGui, inside a folder named Frames.
Screenshot 2022-03-04 at 7.31.23 PM

I recieve the client in a script in ServerScriptService.

I think it should be something like this, as if you put debounce inside the script, it will automatically set the debounce to false the whole time, as it runs, so remain it outside of the function.