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

Why is the IntValue going negative when it's not supposed to?

Asked by 4 years ago

I am currently creating a gun script and there is two values; CurrentAmmo and MaxAmmo. I created this portion of the script:

mouse.Button1Down:Connect(function()
    if enableMouse == true then
        isFull = false
        currentAmmo.Value = currentAmmo.Value - 1
        print(currentAmmo.Value)
        realFrame:TweenSize(UDim2.new(1,0,currentAmmo.Value / maxAmmo.Value),'In','Sine',.1)
        game.StarterPlayer.Fire:Play()
        if currentAmmo.Value <= 0 then
            reload = true
            gunSound.SoundId = emptyAmmoSound
            backgroundFrame.TextLabel.Visible = true
            realFrame.BackgroundColor3 = Color3.fromRGB(255,0,0)
            currentAmmo.Value = 0
        end
    end
end)

However, the CurrentAmmo keeps going down until negative, despite the script telling it not to. Any help please?

2 answers

Log in to vote
0
Answered by 4 years ago

Try this:

if enableMouse == true and currentAmmo.Value<=0 then

I personally believe that little change could help.

Ad
Log in to vote
0
Answered by
synkrio 281 Moderation Voter
4 years ago
Edited 4 years ago

The CurrentAmmo turns negative because there is nothing stopping it from doing so. The reload scripts returns the value to zero but only after the gun is fired. I suggest putting the reload script before the event is fired.

Edit kinda late but if you still need it

mouse.Button1Down:Connect(function()
    if enableMouse == true then
    if currentAmmo.Value > 0 then
        isFull = false
        currentAmmo.Value = currentAmmo.Value - 1
        print(currentAmmo.Value)
        realFrame:TweenSize(UDim2.new(1,0,currentAmmo.Value / maxAmmo.Value),'In','Sine',.1)
        game.StarterPlayer.Fire:Play()
    else
            reload = true
            gunSound.SoundId = emptyAmmoSound
            backgroundFrame.TextLabel.Visible = true
            realFrame.BackgroundColor3 = Color3.fromRGB(255,0,0)
            currentAmmo.Value = maxAmmo.Value
        end
    end
end)
0
Can you show an example please? Sensei_Developer 298 — 4y
0
made an edit synkrio 281 — 4y

Answer this question