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

Gun Reload bug?

Asked by
Kryddan 261 Moderation Voter
9 years ago

I have made a gun that works perfectly fine except the reload part. The problem is that if i have less then 12 of my spareammo and less then 12 ammo in the clip, then if i reload i will get 0 ammo and 0 in spareammo or even less sometimes i got -6 spareammo?

function reload(key)
    key:lower()
    if key == "r" and spareammo >= 1 and not reloading and Ammo <= 11 then
        game.Players.LocalPlayer.PlayerGui.weapongui.ReloadingLabel.Visible = true
        print("Reloading")
        reloading = true
        IsFiring = false
        wait(Reloadtime)
        reloading = false
        --Ammo is set to the value 12 and spareammo is 30 from the start
        spareammo = spareammo + (Ammo - 12)     
        if spareammo >= 12 then
            Ammo = 12
        elseif spareammo <= 11 and spareammo > 0 then
            Ammo = spareammo
        elseif spareammo == 0 then
            Ammo = 0
        end
        game.Players.LocalPlayer.PlayerGui.weapongui.ReloadingLabel.Visible = false

    end
end


mouse.KeyDown:connect(reload)
Gun.Deactivated:connect(stopshooting)
Gun.Activated:connect(shoot)

if you dont understand the problem then i can try to explain it better for you.

1 answer

Log in to vote
1
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
9 years ago

Your problem is the your calculating how much spare ammo there is by subtracting 12 off of your ammo, even if you don't have 12 spare ammo to subtract. Your check can be done a bit better so I cleaned it up for you. Instead of checking when it is between certain intervals, it just keep adding ammo until the magazine is filled or there is no more spare ammo.

function reload(key)
    key:lower()
    if key == "r" and spareammo >= 1 and not reloading and Ammo <= 11 then
        game.Players.LocalPlayer.PlayerGui.weapongui.ReloadingLabel.Visible = true
        print("Reloading")
        reloading = true
        IsFiring = false
        wait(Reloadtime)
        reloading = false

        for i = 1, 12 do -- This is the for loop that determines the ammo
            if ammo < 12 and spareammo > 0 then
                ammo = ammo + 1
                spareammo = spareammo - 1
            end
        end

        game.Players.LocalPlayer.PlayerGui.weapongui.ReloadingLabel.Visible = false
    end
end


mouse.KeyDown:connect(reload)
Gun.Deactivated:connect(stopshooting)
Gun.Activated:connect(shoot)

0
Thanks it works Kryddan 261 — 9y
Ad

Answer this question