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

how to i make an automatic gun?

so basically i have an automatic gun that you can spam and its not able to continue firing when i hold down on the mouse

Mouse.Button1Down:Connect(function()
	if Reloading == false and Ammo.Value > 0 then	

		Ammo.Value = Ammo.Value - 1
		local Position = script.Parent.Parent.Humanoid.TargetPoint
		local looker = (Position - script.Parent.Parent.Head.Position).Unit
		script.Parent.AK53EVENT:FireServer(looker,Ammo)
	elseif Reloading == false and Ammo.Value <= 0 then
		wait(2)
		Ammo.Value = 35
	end
end)

1 answer

✓ Accepted answer

Mouse.Button1Down only fires once. If you want the code to run multiple times, you'll need a loop of some sort. You'll also need to capture when the player stops holding down the button. Here is a quick example of this:

--let's assume you have a tool.Equipped somewhere that sets equipped = true
local depressed
Mouse.Button1Down:Connect(function()
	if equipped then
		depressed = true
		while depressed  and equipped do
			--your code goes in here to fire the weapon
			wait(.2) --The number in parentheses will be your fire rate
		end
	end
end)

--This detects when the player stopped depressing the left mouse button
Mouse.Button1Up:Connect(function()
	depressed = false
end)

This is just a quick example. You can get the same functionality with tool.activated and tool.deactivated as well. Be careful if you attempt to set a cooldown that's verified from the server as there can be time differences because of lag and the local time of the client probably won't match the local time of the server. A good way to get around this is using some sort of time sync service. There is one included in Nevermore by Quenty: Nevermore.

Nevermore is a HUGE library that contains a lot of helpful modules. I highly suggest learning more about it and at the very least you can use it to learn how Quenty solved certain problems.

[EDIT] To make the script stop shooting after it runs out of bullets, you'll need to add a check for that as well. You can do this check on the while loop line, or directly after (doesn't really matter). Here's an edit to the example above:

		while depressed  and equipped and bullets > 0 do
			--your code goes in here to fire the weapon
			wait(.2) --The number in parentheses will be your fire rate
		end

Same code as above just written out differently:

		while depressed  and equipped do
			if bullets > 0 then
				--your code goes in here to fire the weapon
			end
			wait(.2) --The number in parentheses will be your fire rate
		end

5 comments

0

Thanks! But my reloading thing isn't working is there a way that I can make it so when it hits zero it stops working?

0

elseif Ammo.Value <= 0 then Shootable = false depressed = false print("Reloading") wait(2) depressed = true Shootable = true Ammo.Value = 35

0

thats the script

0

I updated the answer

0

thanks! It helps a lot

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