I was making a burst gun for my game, but I cant figure out how to make it work?
Originally, I was planning to just use a for loop, but I wanted to make it so that the mouse position gets updated in each individual shot of the burst. So I implemented this on the client. The problem is that I have a firerate check on the server (for security) and that doesn’t allow this. I was thinking of using some sort of other variable that gets used if burst mode is enabled, but I just cant think of a solution… Heres my code:
Client-SIDE
elseif configuration.FireType.Value == "Burst" then
canFire = false
for index = 1, configuration.BurstShots.Value do
fire()
print("Fired")
task.wait(configuration.BurstDelay.Value)
end
SERVER SIDE
local function onFireEvent(player: Player, mousePosition: Vector3?)
if not canShoot(mousePosition) then
return
end
if (tick() - lastShot) < configuration.RateOfFire.Value then -- issue occurs here
return
end
for index = 1, configuration.NumBullets.Value do -- Ignore this, just for shotguns
fire(mousePosition)
end
ammo.Value -= 1
lastShot = tick()
end
Ok. So I ended up using a variable that works similiar to the lastShot variable except for bursts. This probably isnt the best solution but if it works it works.
local function onFireEvent(player: Player, mousePosition: Vector3?)
if not canShoot(mousePosition) then
return
end
if fireType.Value == "Burst" then
if burstShots == 0 and (tick() - lastBurstShot) < configuration.BurstDelay.Value then
return
end
if burstShots >= configuration.BurstShots.Value then
if (tick() - lastFullBurst) < configuration.RateOfFire.Value then
return
end
burstShots = 0
lastFullBurst = tick()
lastBurstShot = tick()
end
else
if (tick() - lastShot) < configuration.RateOfFire.Value then
return
end
end
for index = 1, configuration.NumBullets.Value do
fire(mousePosition)
end
ammo.Value -= 1
burstShots += 1
lastShot = tick()
if burstShots == 1 then
lastBurstShot = tick()
end
end
Your client fires multiple shots in quick rate, but your server’s RateOfFire check prevents this, as it only allows a shot if the time since the last shot exceeds a certain threshold.
Update the mouse position for each shot and send the server a request for each burst shot separately.
if configuration.FireType.Value == "Burst" then
canFire = false
for index = 1, configuration.BurstShots.Value do
fire() -- Fire each shot
print("Fired shot", index)
task.wait(configuration.BurstDelay.Value) -- Delay between burst shots
end
canFire = true
end
Keep track of burst shots using burstShots and control the delay between bursts
local function onFireEvent(player: Player, mousePosition: Vector3?)
if not canShoot(mousePosition) then
return
end
-- Handle burst shots
if fireType.Value == "Burst" then
if burstShots == 0 and (tick() - lastBurstShot) < configuration.BurstDelay.Value then
return -- Not enough time since last shot in the burst
end
if burstShots >= configuration.BurstShots.Value then
if (tick() - lastFullBurst) < configuration.RateOfFire.Value then
return -- Ensure rate of fire between bursts is respected
end
burstShots = 0
lastFullBurst = tick() -- Track when the last burst finished
end
else
-- the single shot rate of fire check
if (tick() - lastShot) < configuration.RateOfFire.Value then
return
end
end
-- Fire the shot
for index = 1, configuration.NumBullets.Value do
fire(mousePosition)
end
-- handles ammo and burst timing
ammo.Value -= 1
burstShots += 1
lastShot = tick() -- Track individual shot time
if burstShots == 1 then
lastBurstShot = tick() -- tracks the burst shot
end
end