So recently I've been making a role playing game and have made a framework for my guns. So for the ammo I thought to myself "Why not use a debounce to stop the player from firing more bullets until they press 'Reload'?". So I've written the script out but it doesn't seem to work, because after reloading the player is still unable to fire again after depleting their ammo.
local debounce = false
local cooldownTime = 0.7
script.Parent.Activated:Connect(function()
if not debounce then
debounce = true
local shooting = game:GetService("Players").LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Shoot)
local sound = game:GetService("StarterPack").Shotgun.shooting
sound:Play()
shooting:Play()
wait(cooldownTime)
Ammo = Ammo-1
debounce = false
if Ammo < 1 then
debounce = true
else
if Ammo > 1 then
debounce = false
end
end
end
end)
This section of the code is where the player's input (AKA the mouse click) is detected, and where the ammo value is located.
local debounce = false
local eq=false
local cooldownTime = 1
local mouse=game.Players.LocalPlayer:GetMouse()
script.Parent.Equipped:Connect(function(mouse)
eq=true
end)
script.Parent.Unequipped:Connect(function(mouse)
eq=false
end)
mouse.KeyDown:Connect(function(key)
if not debounce and key== "r" and eq then
debounce = true
local reload = game:GetService("Players").LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Reload)
local sound = game:GetService("StarterPack").Shotgun.reloading
reload:Play()
reload.Looped = false
sound:Play()
Ammo = Ammo + 1
wait(cooldownTime)
debounce = false
end
end)
This section is where the player hits R in order to reload and the ammo value is refilled.
Any tips, ideas, additional coding, or a link to a post relating to this kind of stuff would be greatly appreciated!
