What do you want to achieve? While a player holding down a button add 1 to an int value but with debounc beetween pressing Keep it simple and clear!
What is the issue? I have the script that detects it but I tried to add cooldown between pressing but when I spam the button it add more then 1. Any ideas how to fix it? Include screenshots / videos if possible!
What solutions have you tried so far? I tried to add normal debounce but it doesn’t helped Did you look for solutions on the Developer Hub? yes, I did but I couldn’t find anything about it
-- local Script
UIS.InputBegan:Connect(function(input,isTyping)
if isTyping then return end
if input.KeyCode == Enum.KeyCode.R then
if not restDb then
restDb = true
restRemote:FireServer("Begin")
end
end
end)
UIS.InputEnded:Connect(function(input,isTyping)
if isTyping then return end
if input.KeyCode == Enum.KeyCode.R then
if restDb then
restRemote:FireServer("Ended")
end
end
end)
restRemote.OnClientEvent:Connect(function()
wait(restCd)
restDb = false
end)
--ServerScript
restRemote.OnServerEvent:Connect(function(player,Action)
local char = player.Character
local hum = char:WaitForChild("Humanoid")
local energy = player.leaderstats.Energy
local maxEnergy = player.Backpack.Values.MaxEnergy
local hold = player.Backpack.Values.Hold
local holdDb = player.Backpack.Values.HoldDebounce
print("Server")
if Action == "Begin" then
print("Begin")
hold.Value = true
while hold.Value do
energy.Value += 1
wait(1)
end
elseif Action == "Ended" then
print("End")
hold.Value = false
restRemote:FireClient(player)
end
end)
As @hostnod said, you need to make is so when they press the button, release it, and press it again the InputBegan function doesn’t fire, so put the task.wait(1) in that section, not the if Action == "Begin" section.
-- local Script
UIS.InputBegan:Connect(function(input,isTyping)
if isTyping then return end
if input.KeyCode == Enum.KeyCode.R then
restRemote:FireServer("Begin")
end
end
end)
UIS.InputEnded:Connect(function(input,isTyping)
if isTyping then return end
if input.KeyCode == Enum.KeyCode.R then
restRemote:FireServer("Ended")
end
end)
--ServerScript
local CS = game:GetService("CollectionService")
restRemote.OnServerEvent:Connect(function(player,Action)
local char = player.Character
local hum = char:WaitForChild("Humanoid")
local energy = player.leaderstats.Energy
local maxEnergy = player.Backpack.Values.MaxEnergy
local hold = player.Backpack.Values.Hold
local holdDb = player.Backpack.Values.HoldDebounce
print("Server")
if Action == "Begin" and not CS:HasTag(player, "CD") then
CS:AddTag(player, "CD")
print("Begin")
hold.Value = true
while hold.Value do
energy.Value += 1
wait(1)
end
elseif Action == "Ended" then
CS:RemoveTag(player, "CD")
print("End")
hold.Value = false
restRemote:FireClient(player)
end
end)
hold.Value = true
while hold.Value do
energy.Value += 1
wait(1)
end
The issue with your code is that every time restRemote is fired, you immediately increment their energy and then yield. Simply swap the order of energy.Value += 1 and wait(1) and you will find players are no longer able to repeatedly spam click for large amounts of energy:
hold.Value = true
while hold.Value do
task.wait(1)
energy.Value += 1
end
An alternative would be to have some sort of cooldown as @weakroblox35 has mentioned, although I don’t believe the exact code they provided would function.
It’s an alternative to the cooldown, the player won’t fire the begin state if they have the tag and it gets removed if the player ends the “Begin” state. Not to mention it’s for anti-exploiting purposes.
both of those scripts work fine, but if I start spamming it starts bugging somehow and add more then 1
I’m trying to make a charge effect like in shindo life chi charge or something like that.