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

How do I use my while loop properly?

Ok so I made a gun, semi auto works perfectly. I went into trying to make a full auto gun, it works except it doesn’t stop shooting. I am using Mouse.Button1Down I did ~~~~~~~~~~~~~~~~~while Mouse.Button1Down do shoot() wait(.2) end~~~~~~~~~~~~~~~~~

Can anyone tell me how to make it so it only shoots when the left button is down and it stops shooting when I stop holding down the button?

Thanks

1 answer

✓ Accepted answer

Mouse.Button1Down is an event. This means it will fire a function when the mouse button is pressed down. It does not return a boolean value of true or false. You want to make it so that a boolean variable is updated when the mouse is pressed down (or up) and then you can structure your code in a while loop like you have.

This is probably more along the lines of what you want:

local isButton1Down = false;

mouse.Button1Down:connect(function()
	isButton1Down = true;
end);

mouse.Button1Up:connect(function()
	isButton1Down = false;
end);

while (isButton1Down) do 
	shoot();
	wait(.2);
end;

1 comment

0

or, y'know. UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1)

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