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)
i want the CCchange event to fire once and not like 10 - 200 times
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)
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)
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)
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)