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

how to stop my gun from going in negatives?

Asked by 4 years ago
Edited 4 years ago

i recently made a gun but when the clip value is higher than the MaxAmmo Value, MaxAmmo goes in negatives

just like this 12/-4 how do i fix it to where it cant go to negatives and so you cant reload at 0 ammo

this is my reload function:

EDIT: YES I TRIED CHANGING FROM MATH.MIN TO MATH.ABS AND IT DIDNT WORK

local function Reload()
    if not Reloading then
        if SpareAmmo > 12 then
        Reloading = true
        -- Don't reload if you are already full or have no extra ammo
        if AmmoInClip ~= ClipSize and script.Parent.MaxAmmo.Value > 0 then
            if RecoilTrack then

                RecoilTrack:Stop()
            end
            if WeaponGui and WeaponGui:FindFirstChild('Crosshair') then
                if WeaponGui.Crosshair:FindFirstChild('ReloadingLabel') then
                    WeaponGui.Crosshair.ReloadingLabel.Visible = true
                end
            end
            ReloadTrack:Play()
            Reloadd:FireServer(Player)
            wait(ReloadTime)
            -- Only use as much ammo as you have
            local ammoToUse = math.min(ClipSize - AmmoInClip, SpareAmmo)
            AmmoInClip = AmmoInClip + ammoToUse
            script.Parent.MaxAmmo.Value = script.Parent.MaxAmmo.Value - ammoToUse
            UpdateAmmo(AmmoInClip)
                WeaponGui.Reload.Visible = false
            else 
                SpareAmmo = 0
        end
        Reloading = false
        end
        end
end
0
Post your script please User#32819 0 — 4y
0
I edited it so now u can see it TrippeDanieI 21 — 4y
0
Is this your script User#30567 0 — 4y
0
^ nope but i converted it to FE and now im having this issue TrippeDanieI 21 — 4y

2 answers

Log in to vote
1
Answered by 4 years ago

If the values are correct, but the negatives are unnecessary, then use the math.abs function to get the absolute value of the number, meaning it won't be negative as well.

0
so what part do i replace TrippeDanieI 21 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

Here's how I would do it. Once your max ammo is < 0, you can use math.abs and take away the spare ammo from the max ammo. After that, you can set the max ammo to 0. The script below is just an example I made. You can try to implement it yourself.

local magAmmo = 7
local maxAmmo = 6

if magAmmo == 7 then
    return
elseif magAmmo >= 6 then
    magAmmo = 7
    maxAmmo = maxAmmo - 7
    if maxAmmo < 0 then
        magAmmo = magAmmo - math.abs(maxAmmo)
        maxAmmo = 0
    end

However, this scripts assumes that you don't have the Changed or GetPropertyChangedSignal() event. If this does happen though, then just use variables. Everyone's gun scripts (even mine) are different.

Answer this question