I'm currently working on an fps and when I test it with my friends we found a bug where you can still use the gun after you have died. So I need the gun to stop working once your health reaches 0 and it needs to start working again once you respawn.
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(2)
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.DealDamage: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()
local Anime = script.Animation
Anime:Play()
end
end)
end)

3 comments
It didnt work
it is not supposed to work, it is supposed to show you how to do it. I made code example of how you could make fix for your gun.
i know but it still didnt work