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

More efficient ways to check ammo amount in mag?

Asked by 4 years ago

When I deal with muzzle flare, I usually keep the muzzle flaring until the player stops holding down the mouse. But, the muzzle flare keeps going when the gun is not firing and is all out of ammo and the player is still holding down the mouse. I prevent this by having a wait loop that runs every millisecond or something.

The problem is, wait loops can take up a lot of memory and I want to look for a more efficient way of checking ammo. Here’s my code:

while wait() do
    if PrimaryCurrentAmmo < 1 then
          --//Code here
    end
end

Any help would be greatly appreciated!

0
Use the Changed event Pupppy44 671 — 4y
0
Ohh, okay. Let me try it! Cynical_Innovation 595 — 4y
0
@Pupppy44, doesn't work Cynical_Innovation 595 — 4y
0
You probably didn't do it correctly then Pupppy44 671 — 4y
View all comments (2 more)
0
Can you please write an answer on this? Cynical_Innovation 595 — 4y
0
IntValue.Changed:Connect(function(Value) if Value < 1 then something() end end) User#30567 0 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

What I personally did for my gun script is have a Int Value in the tool and have the local script check if the number is above 0. If Mag.Value > 0 then it would activate the muzzle flash function and fire a remote event that would cast a ray.

The code looks something like this:

function Flash()
    local attachment = Handle:FindFirstChild("GunFirePoint")
    local particle = attachment.MuzzleFlash
    particle:Emit(1)
    end
end

if input.UserInputType == Enum.UserInputType.MouseButton1 and Mag.Value > 0 then
        while true do
            MouseEvent:FireServer(FireDirection)
            Flash()
            wait(firedelay)
        end 
0
I already have this in a different part of the script. The problem is that when I hold down the mouse the flare still won't disappear. That's why I use the while wait loop Cynical_Innovation 595 — 4y
0
Oh sorry, maybe you could try doing a "while true do" loop and then under it particle:Emit(1) wait(firerate)? Let me edit it into my post so it's easy to read. LeedleLeeRocket 1257 — 4y
0
No, I already use a while true loop. You're just delaying it even more Cynical_Innovation 595 — 4y
Ad
Log in to vote
-1
Answered by 4 years ago

You could always use a number value

0
That's what I AM USING. besides, that would NOT change anything Cynical_Innovation 595 — 4y

Answer this question