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

How to set Projectile fire rate?

I was making a projectile gun, and I am having trouble with the shooting. It works fine, but I want to make it only shoot once when clicking once, then holding down shoot multiple times. Any advice on how to do that? My script:

local tool = script.Parent
local handle = tool:WaitForChild("Handle")
local barrel = tool:WaitForChild("Barrel")

local cas = game:GetService("ContextActionService")
local rs = game:GetService("ReplicatedStorage")
local fire = false
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

function shoot(actionName, inputstate)
    if inputstate == Enum.UserInputState.Begin then
	fire = true
end
    if inputstate == Enum.UserInputState.End then
	fire = false
end
end

function bullet()
    rs:WaitForChild("GunEvent"):FireServer(tool, barrel.Position, mouse.Hit.p)
    wait(0.5)
end

cas:BindAction("Shoot", shoot, true, Enum.UserInputType.MouseButton1,
               Enum.UserInputType.Gamepad1)

game:GetService("RunService").RenderStepped:Connect(function()
        if fire == true then 
		bullet()
 end
end)

(There is other scripts connected to it to make it work, I'm not sure if I need to send them).

1 answer

I have this code in my gun, that keeps repeating shooting, until it runs out of ammo. Not sure if you have ammo or not. The firing basically keeps the gun shooting, until it runs out of ammo, or the player lifts their mouse up. Your using different input methods, but this works for me!

mouse.Button1Up:Connect(function()
		repeat 
		wait()
		isFiring = false
		until isFiring == false
	end)

    mouse.Button1Down:Connect(function()
		isFiring = true
	repeat
		--if ammo.Value == 0 then 
			--isFiring = false
			--break
		--end
       
			wait()
			
            Shoot()
		
		until isFiring == false --or ammo.Value == 0
    end)

4 comments

0

Yes, ik. When the player lets go of their mouse, isfiring = false, so it stops shooting.

0

No but, thing is., yes your code is a good method, but aswell I want to try experiment with clicking guns. What you just said works with my gun fine, but the problem I am having is, If the player holds the button down, it will fire rapidly, if the player only clicks, it will fire only 1 bullet or so.

0

Add a wait in your shoot function.

0

I tried that, with the bullet function- shoot function would just make the bullets go slower. That's not what I am attempting to do.

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