What I want to achieve is a cooldown meaning, I press Q, the attack happens, but when I press Q again I want the script to wait 3 seconds. The Issue is I have 2 Userinput Events, InputBegain and InputEnded, if I add a debounce say to InputBegain the Inputended Event Still fires.
I tried to add debounces to both event functions, and I also tried to make separate debounces for both of them but have had no success.
local UIS = game:GetService("UserInputService")
local RPS = game:GetService("ReplicatedStorage")
local Remote = RPS:WaitForChild("RemoteEvent")
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Animations = RPS.Animations
local function inputBegain(input,Istyping)
if Istyping then return end
if input.KeyCode == Enum.KeyCode.Q then
Remote:FireServer("Charging")
end
end
local function inputEnded(input,IsTyping)
if IsTyping then return end
if input.KeyCode == Enum.KeyCode.Q then
Remote:FireServer("Done")
end
end
local function Client(Data)
local ChargingPose = Humanoid.Animator:LoadAnimation(Animations:WaitForChild("ChargingPose"))
if Data == "Charging" then
ChargingPose:Play()
print("Charging")
elseif Data == "Done" then
ChargingPose.AnimationTrack:Stop()
print("Charging is completed bro")
end
end
UIS.InputBegan:Connect(inputBegain)
UIS.InputEnded:Connect(inputEnded)
Remote.OnClientEvent:Connect(Client)
```lua
1. ```
local RPS = game:GetService("ReplicatedStorage")
local Remote = RPS:WaitForChild("RemoteEvent")
local function ServerCharge(Player,Data)
if Data == "Charging" then
Remote:FireClient(Player,"Charging")
elseif Data == "Done" then
Remote:FireClient(Player,"Done")
end
end
Remote.OnServerEvent:Connect(ServerCharge)
Why not just creating a debounce server-sided? Something like this:
local RPS = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Remote = RPS:WaitForChild("RemoteEvent")
local wait_time = 3
local PlayersDebounce = {}
local function ServerCharge(Player,Data)
if tick() - PlayersDebounce[Player] < wait_time then return end
PlayersDebounce[Player] = tick()
if Data == "Charging" then
Remote:FireClient(Player,"Charging")
elseif Data == "Done" then
Remote:FireClient(Player,"Done")
end
end
Players.PlayerAdded:Connect(function(player)
if not PlayersDebounce[player] then
PlayersDebounce[player] = tick()
else
return
end
end)
Players.PlayerRemoving:Connect(function(player)
PlayersDebounce[player] = nil
end)
Remote.OnServerEvent:Connect(ServerCharge)
Wouldn’t this work? (I could be completely out to lunch here, just writing this quickly at work)
local function inputBegain(input,Istyping)
if Istyping then return end
if debounce = false then
while input.KeyCode == Enum.KeyCode.Q do
Remote:FireServer("Charging")
task.wait(.1)
end
debounce = true
end
end
and put a debounce = false in the line where the function is called to make it work only the first time you hold Q down.
EDIT, dangit that won’t work, you need to check the debounce when you call the function to see if q is already being pressed so the charge function doesn’t get called more than once.
local RPS = game:GetService("ReplicatedStorage")
local Remote = RPS:WaitForChild("RemoteEvent")
local Cooldowns = {}
local Debounce = false
local function ServerCharge(Player,Data)
if Data == "Charging" then
if Cooldowns[Player] then return end
Cooldowns[Player] = true
Remote:FireClient(Player,"Charging")
elseif Data == "Done" and not Debounce then
Debounce = true
Remote:FireClient(Player,"Done")
task.delay(2, function()
Debounce = false
Cooldowns[Player] = false
end)
end
end
Remote.OnServerEvent:Connect(ServerCharge)