I would like to improve this script to be able to do burst fires.
if self.clip <= 0 then return end
if inputState == Enum.UserInputState.Begin then
if self.Config.fireMode == "Burst" and self.canShoot and not self.equip then
--fire server
local direction = castMouseRay(self)
onFire:FireServer(self.Weapon, direction)
--handle varibles
self.reloadAnim:Stop()
self.reloadSound:Stop()
self.fireAnim:Play()
self.canShoot = false
self.isShooting = true
self.isReloading = false
if self.reloadConnection then
self.reloadConnection:Disconnect()
end
--shell
_newShell(self)
--screen shake
self.camShake:Start()
local fireShake = self.camShake:ShakeOnce(self.Config.fireShakeAmount, self.Config.fireShakeRoughness, 0, self.Config.fireShakeDuration)
--update ammo and hud
self.clip -= 1
_setupHUD(self, true)
--when clip size is equal to 0 call reload
if self.clip <= 0 then
self:Reload("", Enum.UserInputState.Begin)
end
task.delay(self.Config.fireDelay, function()
self.canShoot = true
self.isShooting = false
end)
end
end
I am not exactly sure what you want to achieve, but from what I see in your code, you have a remote event for handling the actual firing; all other effects are done on the client. The simplest you can think of is to just fire the remote three times, but why? You can do it with a single remote call.
You either add an argument to your current remote, which will be for the type of fire.
OR
You make a separate remote only for burst firing.
Which way you do it is up to you. For the client part, just make it repeat the effects. You probably know the time between each burst of fire, how long your animations are, and other stuff. Just make sure you check for stuff like whether the player even has a burst weapon and other self-explanatory stuff. This is the way I would do it. Other people might have better suggestions.
Hello! I added a loop to the code, but the delay is not working and when spamming the mouse button seems to overflow the ammo.
if self.clip <= 0 then return end
if inputState == Enum.UserInputState.Begin then
if self.Config.fireMode == "Burst" then
self.canShoot = false
self.isShooting = true
for i = 1, 3 do -- fire 3 times
local direction = castMouseRay(self)
onFire:FireServer(self.Weapon, direction)
self.canShoot = false
-- handle variables
self.reloadAnim:Stop()
self.reloadSound:Stop()
self.fireAnim:Play()
self.isReloading = false
self.canShoot = false
if self.reloadConnection then
self.reloadConnection:Disconnect()
end
-- shell
_newShell(self)
-- screen shake
self.camShake:Start()
local fireShake = self.camShake:ShakeOnce(self.Config.fireShakeAmount, self.Config.fireShakeRoughness, 0, self.Config.fireShakeDuration)
-- update ammo and hud
self.clip -= 1
_setupHUD(self, true)
-- when clip size is equal to 0, call reload
if self.clip <= 0 then
self:Reload("", Enum.UserInputState.Begin)
end
task.wait(self.Config.fireDelay, function()
task.delay(self.Config.fireDelay, function()
self.canShoot = true
end)
end)
end
end
end
Why don’t you keep track of how many bullets have been fired since the player pressed MB1 and have a debounce that won’t allow them to fire until numberOfBurstBulletsFired == 3
-- Inside your shooting function:
if self.clip <= 0 or self.bulletsShot > 0 then return end
if inputState == Enum.UserInputState.Begin then
if self.Config.fireMode == "Burst" and self.canShoot and not self.equip then
for i = 1, 3 do
--fire server
local direction = castMouseRay(self)
onFire:FireServer(self.Weapon, direction)
--handle variables
self.reloadAnim:Stop()
self.reloadSound:Stop()
self.fireAnim:Play()
self.canShoot = false
self.isShooting = true
self.isReloading = false
if self.reloadConnection then
self.reloadConnection:Disconnect()
end
--shell
_newShell(self)
--screen shake
self.camShake:Start()
local fireShake = self.camShake:ShakeOnce(self.Config.fireShakeAmount, self.Config.fireShakeRoughness, 0, self.Config.fireShakeDuration)
--update ammo and hud
self.clip -= 1
_setupHUD(self, true)
task.wait(self.Config.fireDelay)
end
-- Reset bullets shot counter and initiate reload after firing all bullets
self.bulletsShot = 0
task.wait(self.Config.reloadDelay, function()
self:Reload("", Enum.UserInputState.Begin)
end)
end
end
Thanks! But tried it and got an error.
It says:
ContextActionService: Unexpected error while invoking callback: ReplicatedStorage.Modules.Framework:411: attempt to compare nil <= number - Studio ReplicatedStorage.Modules.Framework:411: attempt to compare nil <= number
on the client check if the player is holding left mouse button then fire a remote event and debounce (on the server), on server do a for loop with the amount of iteration being the amount of bullets one burst has, task.wait to delay each shot, once the burst finishes undebounce the gun.
114 →--constructor for a new gun
function GunFramework.new(tool, config)
local self = {}
setmetatable(self, GunFramework)
411→ if self.clip <= 0 or self.bulletsShot <= 0 then return end
if inputState == Enum.UserInputState.Begin then
if self.Config.fireMode == "Burst" and self.canShoot and not self.equip then
for i = 1, 3 do
--fire server
local direction = castMouseRay(self)
onFire:FireServer(self.Weapon, direction)