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

Ammo going in the negatives?

So i'm making a gun and it's going good but i have a problem where if all of the ammo is a certain number then it's possible for it to go to the negatives. I've tried using math.floor but that didn't really work for me. I also tried checking the number with an if statement but it's really confusing and i don't really know how to fix it! Any help at all would be nice

Here is the part of the script that reloads which is where it needs fixing.

	uis.InputBegan:Connect(function(input)
		if script.Parent.Parent.Parent == char then
			if input.KeyCode == Enum.KeyCode.R then
				if script.Parent.Parent.Ammo.Value ~= script.Parent.Parent.MaxAmmo.Value and script.Parent.Parent.AllAmmo.Value > 0 then
					if reloaddebounce == false then
						reloaddebounce = true
						script.Parent.Parent.Sounds.Reload:Play()
						reload:Play()
						wait(3)
						script.Parent.Parent.Ammo.Value = script.Parent.Parent.MaxAmmo.Value
						script.Parent.Parent.AllAmmo.Value = script.Parent.Parent.AllAmmo.Value - script.Parent.Parent.AmmoLost.Value
						script.Parent.Parent.AmmoLost.Value = 0
						reloaddebounce = false
					end
				end
			end
		end
	end)

1 comment

0

Why are you refercing two different values for your ammo? You reference Ammo.Value for one and AllAmmo.Value for the other. I'm assuming these mean different things because you reference both later on. You should add another and statement checking if script.Parent.Parent.Ammo.Value is greater than 0.

1 answer

You need to add this: script.Parent.Parent.AllAmmo.Value == 0 to script.

--Fixed Code:
uis.InputBegan:Connect(function(input)
    if script.Parent.Parent.Parent == char then
        if input.KeyCode == Enum.KeyCode.R then
            if script.Parent.Parent.Ammo.Value ~= script.Parent.Parent.MaxAmmo.Value and script.Parent.Parent.AllAmmo.Value > 0 or script.Parent.Parent.AllAmmo.Value == 0 then --Here
                if reloaddebounce == false then
                    reloaddebounce = true
                    script.Parent.Parent.Sounds.Reload:Play()
                    reload:Play()
                    wait(3)
                    script.Parent.Parent.Ammo.Value = script.Parent.Parent.MaxAmmo.Value
                    script.Parent.Parent.AllAmmo.Value = script.Parent.Parent.AllAmmo.Value - script.Parent.Parent.AmmoLost.Value
                    script.Parent.Parent.AmmoLost.Value = 0
                    reloaddebounce = false
                end
            end
        end
    end
end)

If still doesn't works, Post a comment.

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