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

My gun will shoot as fast as I can click, but when I hold it, it shoots every second?

I'm trying to make my gun shoot every second, even if you're not holding down the mouse. But I can shoot as fast as I click, does anyone see a problem in my script? Do I need to add another wait somewhere in it or another function?

--//Variables
local plr = game.Players.LocalPlayer
local tool = script.Parent.Parent
local hole = tool.Hole
local handle = tool.Handle
local debounce = true
local config = tool.Config
local range = config.Range
local dmg = config.Damage
local coolDown = config.CoolDown
local clips = config.Clips
local ammo = config.Ammo
local maxAmmo = config.MaxAmmo
local allowTracing = config.AllowTracing
local reloadTime = config.ReloadTime
local isReloading = false
local firing = false
--//Sounds
local reloadSound = tool.Handle.ReloadSound
local fireSound = tool.Handle.FireSound
local equipSound = tool.Handle.EquipSound
local emptySound = tool.Handle.EmptySound
 
--//Events
tool.Equipped:Connect(function(mouse)
    equipSound:Play()
    tool.Activated:Connect(function()
	firing = true
	repeat
		wait()
        if not isReloading then
			if firing == true then
            if ammo.Value > 0 then
                if debounce then
                    --//Play sound
                    fireSound:Play()
                    --//Doesn't allow spamming
                    debounce = true
                    --//Ray Get, Set
                    local ray = Ray.new(hole.CFrame.p, (mouse.Hit.p - hole.CFrame.p) * range.Value)
                    local hit, position = workspace:FindPartOnRay(ray, plr.Character, false, true)
                   
                    --//Bullet Tracing
                    if allowTracing.Value == true then
                        --//Make part
                        local trace = Instance.new("Part", workspace)
                        trace.Material = Enum.Material.Neon
                        trace.BrickColor = BrickColor.new("Black")
                        trace.CanCollide = false
                        trace.Anchored = true
                        trace.Transparency = 0.5
                       wait()
                        --//Show Direction
                        local distance = (hole.CFrame.p - position).magnitude
                        trace.Size = Vector3.new(0.2, 0.2, distance)
                        trace.CFrame = CFrame.new(hole.CFrame.p, position) * CFrame.new(0, 0, -distance/2)
                       
                        --//Remove debris
                        game:GetService("Debris"):AddItem(trace, 0.1)
                    end
                   
                    --//Hit Detection
                    if hit then
                        local humanoid =  hit.Parent:FindFirstChild("Zombie")
                        if humanoid then
                            if hit.Name == "Head" then
                                --//Double damage on headshots
                                humanoid:TakeDamage(dmg.Value*2)
                            else
                                --//Normal Damage on body shots
                                humanoid:TakeDamage(dmg.Value)
                            end
                        end
                    end
                    ammo.Value = ammo.Value - 1
                    wait(coolDown.Value)
                    debounce = true
                end
            else
	wait()
                --//Out of ammo
               -- emptySound:Play()
  end          end        
end
--if ammo == 0 then 
--firing = false
--end
mouse.Button1Up:Connect(function()
	wait()
firing = false
end)
	
until ammo == 0 or firing == false
	end)
    --//Reload Event
    
	--mouse.KeyDown:Connect(function(key)
        --key:lower()
       -- if key == "r" then
	mouse.Button1Down:Connect(function(click)
            if not isReloading then
                if clips.Value > 0 then
					if ammo.Value == 0 then
                    isReloading = true
					reloadSound:Play()
                    wait(reloadTime.Value)
					ammo.Value = maxAmmo.Value
                    clips.Value = clips.Value - 1
                    isReloading = false
                end
            end       
end
   end)
end)

1 answer

✓ Accepted answer

First off, you should take the Activated event out of the equipped event. Every time the weapon is equipped a new event will be started which could cause shooting problems and lag when the client equips it enough times. And for the problem, you set debounce to true on line 38, when you should be setting it to false. I didn't fully look through the code for anything that might cause more problems, but if you have any, just comment below.

3 comments

0

I didn't notice the debounce set to true there, and if I get rid of the activated event, it just stops shooting randomly. So I put it back and now its working fine, not sure why.

0

I didn't mean to get rid of the activated event. but to move it and the code inside it onto its own line rather than having it inside the equipped event

0

Thank you, it's working fine now.

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