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

Ammo going in the negatives?

Asked by 4 years ago
Edited 4 years ago

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)
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. gladii 2 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

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.

Ad

Answer this question