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!
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
You could always use a number value