Hello. I have a question about how to make a shooting cooldown for a gun.
This is my current main script:
local maxAmmo = 10
local Ammo = maxAmmo
local reloading = false
local player = game.Players.LocalPlayer
local playerGui = player:WaitForChild(“PlayerGui”)
local textLabel = playerGui:WaitForChild(“AmmoDisplay”):FindFirstChild(“AmmoText”)
script.Parent.Equipped:Connect(function(Mouse)
local function reload()
reloading = true
wait(1)
Ammo = maxAmmo
reloading = false
end
script.Parent.Activated:Connect(function()
if Ammo > 0 and not reloading then
Ammo = Ammo - 1
script.Parent.GunShot:Play()
if Mouse.Target.Parent:FindFirstChild("Humanoid") then
script.Parent.Fire:FireServer(Mouse.Target.Parent, 35)
end
elseif reloading == false then
reload()
script.Parent.GunShot:Stop()
end
while wait() do
textLabel.Text = (Ammo).." / "..maxAmmo
end
end)
local Input = game:GetService("UserInputService")
Input.InputBegan:Connect(function(Key)
if Key.KeyCode == Enum.KeyCode.R and reloading == false and Ammo ~= maxAmmo then
reload()
end
end)
What you could do is make a lastShot variable, in which you store the tick() return of your, well, last shot. Then, before firing you would check if either lastShot hasn’t been set or if tick() subtracted to your lastShot is greater than your firerate before allowing the weapon to fire.
If by shooting cool down you mean a slower firerate then you can just have a debounce variable, similarly to how you handled reloading.
An important thing to note is that this appears to be a clientside script, which makes it vulnerable to exploits. An exploiter can bypass the cool down and ammo check by firing the remote on their own. You might want to add a server side check for this.
You also don’t need to constantly check and update the ammo count UI, you can just adjust it with every shot. I believe that by using a while wait() loop you’re making every single shot infinitely refresh the ammo count UI, which is going to cause a lot of problems.
You need to make a local of true and false name it to ‘RatefireCheck’ to true and make another local name ‘RateOfFire’ and put what you want cooldown on RateOfFire like task.wait(1) or wait(1), and then add the code in activating the tool is to check if the RatefireCheck is true it will fire if its false it wont shoot after activating the tool make the RatefireCheck to false then add task.wait(RateOfFire) after this cooldown make the RatefireCheck to true again(RatefireCheck is to use the activate function script is to check first the RatefireCheck if its true or false)
Reminder:
Sorry for my grammar please remind me if you need a script thanks :D.
heres the code with debounce this should solve your problem
script.Parent.Activated:Connect(function()
if Ammo > 0 and not reloading then
Ammo = Ammo - 1
script.Parent.GunShot:Play()
if Mouse.Target.Parent:FindFirstChild("Humanoid") then
script.Parent.Fire:FireServer(Mouse.Target.Parent, 35)
end
local Debounce = false
elseif reloading == false then
reload()
script.Parent.GunShot:Stop()
if Debounce then return end
Debounce = true
while wait() do
textLabel.Text = (Ammo).." / "..maxAmmo
Debounce = false
end
end)
local Input = game:GetService("UserInputService")
Input.InputBegan:Connect(function(Key)
if Key.KeyCode == Enum.KeyCode.R and reloading == false and Ammo ~= maxAmmo then
reload()
end
end)
I tried a tutorial. It worked but as soon as I shoot my ammo gui goes to 9 and automatically back to 10.
Here’s the script:
Debounce = true
local maxAmmo = 10
local Ammo = maxAmmo
local reloading = false
local player = game.Players.LocalPlayer
local playerGui = player:WaitForChild(“PlayerGui”)
local textLabel = playerGui:WaitForChild(“AmmoDisplay”):FindFirstChild(“AmmoText”)
script.Parent.Equipped:Connect(function(Mouse)
local function reload()
reloading = true
wait(1)
Ammo = maxAmmo
reloading = false
end
script.Parent.Activated:Connect(function()
if Ammo > 0 and not reloading then
Ammo = Ammo - 1
script.Parent.GunShot:Play()
if Debounce == true then
Debounce = false
if Mouse.Target.Parent:FindFirstChild("Humanoid") then
script.Parent.Fire:FireServer(Mouse.Target.Parent, 35)
wait(2)
Debounce = true
end
elseif reloading == false then
reload()
script.Parent.GunShot:Stop()
while wait() do
textLabel.Text = (Ammo).." / "..maxAmmo
end
end
end
local Input = game:GetService("UserInputService")
Input.InputBegan:Connect(function(Key)
if Key.KeyCode == Enum.KeyCode.R and reloading == false and Ammo ~= maxAmmo then
reload()
end
end)
end)
end)
I guarentee theres a way to fix this poblem, i just dont really know how to fix it, but if you keep playing around with it, it should work at some point