Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

how to stop my gun from going in negatives?

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

4 comments

0

Post your script please

0

I edited it so now u can see it

0

Is this your script

0

^ nope but i converted it to FE and now im having this issue

2 answers

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.

1 comment

0

so what part do i replace

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.