shooting = false debounce = false UIS = game:GetService('UserInputService') UIS.InputBegan:connect(function(input) if input.UserInputType == Enum.UserInputType.MouseButton1 and interacting.Value == false then shooting = true while shooting do if gameCharp.Value == 1 then CannonShot() end end end end) UIS.InputEnded:connect(function(input) if input.UserInputType == Enum.UserInputType.MouseButton1 then shooting = false end end) mouse = player:GetMouse() function CannonShot() if not debounce then debounce = true local rng = 1.2 local dmg = 40 local rte = 1.5 local spd = 20 local bullet = Instance.new('Part', workspace) bullet.CanCollide = false bullet.FormFactor = Enum.FormFactor.Custom bullet.Size = Vector3.new(0.2, 0.2, 0.2) bullet.Transparency = 1 bullet.Position = player.Character.Head.Position bullet.CFrame = CFrame.new(player.Character.Head.Position, mouse.Hit.p) bullet.Velocity = bullet.CFrame.lookVector * spd Debris:AddItem(bullet, rng) local trail = workspace.Particles.CannonShot.ParticleEmitter:Clone() trail.Parent = bullet local velocity = Instance.new('BodyForce', bullet) velocity.force = Vector3.new(0, bullet.Size.X * bullet.Size.Y * bullet.Size.Z * 196.2, 0) local hitbox = Instance.new('Part', workspace) hitbox.FormFactor = Enum.FormFactor.Custom hitbox.Size = Vector3.new(0.2, 5.2, 0.2) hitbox.CanCollide = false hitbox.Position = player.Character.Head.Position hitbox.Velocity = Vector3.new(0, 0, spd) hitbox.Transparency = 1 Debris:AddItem(hitbox, rng) local velocity2 = Instance.new('BodyForce', hitbox) velocity2.force = Vector3.new(0, hitbox.Size.X * hitbox.Size.Y * hitbox.Size.Z * 196.2, 0) hitbox.Touched:connect(function(hit) if hit.Parent:FindFirstChild('Humanoid') and hit.Parent.Humanoid.Team.Value ~= player.Character.Humanoid.Team.Value then hit.Parent.Humanoid:TakeDamage(dmg) else end end) wait(rte) debounce = false end end
I was in the process of making my attack autorepeat when holding down the mouse but saw that you can still attack much faster than the cooldown is by spamming click, so I attempted to add a debounce to it.
However, if I attack again before the previous bullet has despawned, it crashes. This doesent happen without the debounce no matter how bad i spam it, so I was wondering what might have caused this?
Line 07
There is no wait function, If no wait function is shown on a While
loop, your Studio will crash.
shooting = false debounce = false UIS = game:GetService('UserInputService') UIS.InputBegan:connect(function(input) if input.UserInputType == Enum.UserInputType.MouseButton1 and interacting.Value == false then shooting = true while shooting do wait() if gameCharp.Value == 1 then CannonShot() end end end end) UIS.InputEnded:connect(function(input) if input.UserInputType == Enum.UserInputType.MouseButton1 then shooting = false end end) mouse = player:GetMouse() function CannonShot() if not debounce then debounce = true local rng = 1.2 local dmg = 40 local rte = 1.5 local spd = 20 local bullet = Instance.new('Part', workspace) bullet.CanCollide = false bullet.FormFactor = Enum.FormFactor.Custom bullet.Size = Vector3.new(0.2, 0.2, 0.2) bullet.Transparency = 1 bullet.Position = player.Character.Head.Position bullet.CFrame = CFrame.new(player.Character.Head.Position, mouse.Hit.p) bullet.Velocity = bullet.CFrame.lookVector * spd -- bullet.Velocity = Vector3.new(0, 0, spd) Debris:AddItem(bullet, rng) local trail = workspace.Particles.CannonShot.ParticleEmitter:Clone() trail.Parent = bullet local velocity = Instance.new('BodyForce', bullet) velocity.force = Vector3.new(0, bullet.Size.X * bullet.Size.Y * bullet.Size.Z * 196.2, 0) local hitbox = Instance.new('Part', workspace) hitbox.FormFactor = Enum.FormFactor.Custom hitbox.Size = Vector3.new(0.2, 5.2, 0.2) hitbox.CanCollide = false hitbox.Position = player.Character.Head.Position hitbox.Velocity = Vector3.new(0, 0, spd) hitbox.Transparency = 1 Debris:AddItem(hitbox, rng) local velocity2 = Instance.new('BodyForce', hitbox) velocity2.force = Vector3.new(0, hitbox.Size.X * hitbox.Size.Y * hitbox.Size.Z * 196.2, 0) hitbox.Touched:connect(function(hit) if hit.Parent:FindFirstChild('Humanoid') and hit.Parent.Humanoid.Team.Value ~= player.Character.Humanoid.Team.Value then hit.Parent.Humanoid:TakeDamage(dmg) else end end) wait(rte) debounce = false end end