Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

How do I make my gun stop working after death?

Asked by 12 days ago
Edited 12 days ago

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)

1 answer

Log in to vote
0
Answered by
imKirda 1203 Moderation Voter
12 days ago
Edited 12 days ago

Humanoid of player's character has an event called Died, you can connect it to a function and disable the gun every time it fires. Example:

local gunEnabled = true
local humanoid = player.Character.Humanoid

humanoid.Died:Connect(function()
    gunEnabled = false
end)

To enable it after respawning, playerhas an event called CharacterAdded, it fires every time the character is added.

player.CharacterAdded:Connect(function(character)
    -- character in this case is the new character that has been added
    gunEnabled = true
end)
0
It didnt work WheatMealLoaf -5 — 12d
0
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. imKirda 1203 — 12d
0
i know but it still didnt work WheatMealLoaf -5 — 12d
Ad

Answer this question