Remote event firing a lot of times when its supposed to fire once

Im making a upgrade script and when im pressing buy its buying once but the boost script fires multiple times and i get the wrong boost

LocalScript:

local db = true
		upgradeValues:FindFirstChild("ChrystalCoinUpgrade").Changed:Connect(function()
			if db == true then
				db = false
				print(x.short(upgradeValues:FindFirstChild("ChrystalCoinUpgrade").Value))
				game.ReplicatedStorage.Remotes.CCChange:FireServer()
				wait(0.25)
				db = true
			end
		end)

ServerScript:

ame.ReplicatedStorage.Remotes.CCChange.OnServerEvent:Connect(function(plr)
	local db = true
	
	if db == true then
		db = false
		local x = require(game.ReplicatedStorage.Data.EternityNum)
		local stats = plr:WaitForChild("Stats")
		local upgrades = plr:WaitForChild("Upgrades")

		stats:FindFirstChild("CurrentCCboost").Value = x.bnumtostr(x.mul(stats:FindFirstChild("CurrentCCboost").Value, x.add(1, x.mul(upgrades:FindFirstChild("ChrystalCoinUpgrade").Value, 0.25))))
		print(stats:FindFirstChild("CurrentCCboost").Value)
		
		task.wait(0.34)
		db = true
	end
end)

Screenshot 2023-01-21 224038

i want the CCchange event to fire once and not like 10 - 200 times

1 Like

This debounce wont do anything, because a new debounce is created every event. First trace back where the events came from, because clearly something is going wrong here.

Now we are going to make a player debounce:

local debounces = {}

To add a player to the debounce use

debounces[player.Name] = true

To check if they got a debounce use

if debounces[player.Name] then

So it would be

local debounces = {}

...:Connect(function(plr)
   if debounces[plr.Name] then
      debounces[plr.Name] = true
      ...
      debounces[plr.Name] = nil
   end
end)

so it would more like this?

local PlayerDebounces = {}

game:GetService("Players").PlayerAdded:Connect(function(plr)
	if plr then
		PlayerDebounces[plr.Name] = true
	end
end)


game.ReplicatedStorage.Remotes.CCChange.OnServerEvent:Connect(function(plr)
	if PlayerDebounces[plr.Name] then
		PlayerDebounces[plr.Name] = true
		local x = require(game.ReplicatedStorage.Data.EternityNum)
		local stats = plr:WaitForChild("Stats")
		local upgrades = plr:WaitForChild("Upgrades")

		stats:FindFirstChild("CurrentCCboost").Value = x.bnumtostr(x.mul(stats:FindFirstChild("CurrentCCboost").Value, x.add(1, x.mul(upgrades:FindFirstChild("ChrystalCoinUpgrade").Value, 0.25))))
		print(stats:FindFirstChild("CurrentCCboost").Value)
		PlayerDebounces[plr.Name] = nil
	end
end)

No keep the script as I showed you. The following part will block all responses remove that first of all

Then add a wait before removing the debounce.

My bad, this part

should have been

if not ... then
1 Like

so im removing:

game:GetService("Players").PlayerAdded:Connect(function(plr)
	if plr then
		PlayerDebounces[plr.Name] = true
	end
end)

but where should i have this?

1 Like

That part is unneeded, the script should look something like this:

local debounces = {}

-- Signal you want to connect it to
...:Connect(function(plr)
   -- if the debounce is inactive
   if not debounces[plr.Name] then
      debounces[plr.Name] = true -- turn the debounce on

      ... -- script to handle everything

      task.wait(5) -- 5 seconds cooldown
      debounces[plr.Name] = nil -- turn the debounce back off
   end
end)
1 Like

ok i understand now, so i can put it under the on event function

local PlayerDebounces = {}

game.ReplicatedStorage.Remotes.CCChange.OnServerEvent:Connect(function(plr)
	
	PlayerDebounces[plr.Name] = true
	
	if not PlayerDebounces[plr.Name] then
		PlayerDebounces[plr.Name] = true
		local x = require(game.ReplicatedStorage.Data.EternityNum)
		local stats = plr:WaitForChild("Stats")
		local upgrades = plr:WaitForChild("Upgrades")

		stats:FindFirstChild("CurrentCCboost").Value = x.bnumtostr(x.mul(stats:FindFirstChild("CurrentCCboost").Value, x.add(1, x.mul(upgrades:FindFirstChild("ChrystalCoinUpgrade").Value, 0.25))))
		print(stats:FindFirstChild("CurrentCCboost").Value)
		wait(5)
		PlayerDebounces[plr.Name] = nil
	end
end)
1 Like

should i not have like a table.insert?

Remove that part, that will block the event from running.

1 Like

No, because then we would have to combine table.remove() and table.find() to change it, which is more annoying to use.

Instead we use a dictionary instead of an array

ok ok so i think i get a lot more rn

so it checks for the player in the table when the remote is activated but since its empty it detects that ur not in it and then it adds u into it?

1 Like

Yes, it basically works like a normal debounce e.g.:

local db = false

...:Connect(function()
   if not db then
      db = true
      ...
      task.wait(5)
      db = false
   end
end)

Though if we would do that we would block other player’s remotes as well. So we use a dictionary to make it player specific.

ok i will test it out now . . .

it worked thank you but can i make the cooldown lower?

Change this part, change the 5 from a 1, etc.

1 Like

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