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

More efficient ways to check ammo amount in mag?

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!

6 comments

0

Use the Changed event

0

Ohh, okay. Let me try it!

0

@Pupppy44, doesn't work

0

You probably didn't do it correctly then

0

Can you please write an answer on this?

0

IntValue.Changed:Connect(function(Value) if Value < 1 then something() end end)

2 answers

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 

3 comments

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

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.

0

No, I already use a while true loop. You're just delaying it even more

You could always use a number value

1 comment

0

That's what I AM USING. besides, that would NOT change anything

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