I made a script to shoot a gun in a very simple way, being automatic for my M4A1 and a upcoming project however when not holding mouse1 it keeps shooting. Here is my code:
local mouse = game.Players.LocalPlayer:GetMouse() -- mouse varible
script.Parent.Activated:Connect(function() --connects the function
wait(0.1) -- the wait time between each shot
while true do -- my way of looping it
wait(0.08)
script.Parent.Fire:FireServer(mouse.Hit.Position) --fires to server
wait(0.08)
end
wait(0.1)
end)
I'm only showing the local script since that is what checks to see if mouse1 is down. I think its beacuse of "while true do" and I know there are many better ways of looping but I have no idea how to do them. Does "while true do" mean while the script is TRUE (enabled) or the function is happening? I tried a few things:
I changed "while true do" to "while mouse.button1down == true do"
I added :
while true do
if mouse.button1down then
--Rest of the code
but for some reason while trying nr. 2. i got this error: "Expected identifer got ")"" And with nr. 1. it didn't shoot at all. Thanks.

1 comment
I wouldn't use the old mouse functions/events from the LocalPlayer. Use UserInputService.