Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

It appears that the gun breaks when It gets unequipped and when the user with the gun dies?

Asked by 2 years ago

Hello! I made a gun that has 3 ammo and one-shots the victim, It works just fine, but when I unequip It, the ammo skips ammo's (Example: When I equip It once It removes 1 ammo per shoot, but when I unequip It and reequip It skips one bullet, It skips alot of bullets If the maxbullet Is higher, such as 25) and when I die or If someone else dies with the gun, the gun just stops working, It won't shoot

More Information: The script Is a local script The ammoGUI Is In PlayerGUI The gun I scripted was thanks to this user: https://www.youtube.com/watch?v=g5WWDFWsi6A

Script:

local maxammo = 3
local ammo = maxammo
local reloading = false
local plr = game.Players.LocalPlayer
local plrgui = plr:WaitForChild("PlayerGui")
local text = plrgui:WaitForChild("Ammo"):FindFirstChild("AmmoCount")
local ShootingAnimation = script.Parent.Animation
local ReloadAnimation = script.Parent.Reload

script.Parent.Equipped:Connect(function(Mouse)
    local function reload()
        reloading = true
        script.Parent.reloadingsound:Play() -- change reloadingsound to what ever your reload sound is named
        local Char = script.Parent.Parent
        local Hum = Char.Humanoid

        local AnimationTrack = Hum:LoadAnimation(ReloadAnimation)
        AnimationTrack:Play()
        wait(1)
        ammo = maxammo
        reloading = false
    end

    script.Parent.Activated:Connect(function()
        if ammo > 0 and not reloading then
            local Char = script.Parent.Parent
            local Hum = Char.Humanoid

            local AnimationTrack = Hum:LoadAnimation(ShootingAnimation)
            AnimationTrack:Play()
            ammo = ammo - 1
            script.Parent.gunshot:Play() -- change gunshot to what ever your gun shot sound is named
            if Mouse.Target.Parent:FindFirstChild("Humanoid") then
                script.Parent.DealDamage:FireServer(Mouse.Target.Parent, 100000000000000000000) -- change the 25 to what ever amount of damage you want to deal
            end
        elseif reloading == false then
            reload()
            script.Parent.gunshot:Stop()-- change gunshot to what ever your gun shot sound is named
        end

        while wait() do
            text.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)
end)

script.Parent.Equipped:Connect(function()
    game.Players.LocalPlayer.PlayerGui.Ammo.Enabled = true
    script.Parent.Unequipped:Connect(function()
        game.Players.LocalPlayer.PlayerGui.Ammo.Enabled = false
    end)
end)

Answer this question