as some people may know, i am extremely horrible at making my code exploit-proof
my current issue is automatic guns, not semi, because those are really simple to code and exploit-proof
but the issue with automatic guns is that, the code they’ve got needs to run consistently while bypassing the sanity checks that are put on the server
now, i do have a current method for this, but something tells me it’s not that good, and since i am reorganizing my gun code, i figured i’d just ask on here
my current method is:
- whenever client does LMB down, there’s an attribute called “IsHolding” that gets set to true on the server, and to false when the client lets go of LMB
- remote event with mouse.hit.position as a parameter
- if the gun is an automatic, fire a remote event called “UpdateHitPosEvent” with an updated hit position
- while the IsHolding attribute is set to true, there’s a codeblock that just repeats
i came here to ask if there’s better methods for this
my current code looks like this:
server script
shootEvent.OnServerEvent:Connect(function(player, initialHitPos)
local tool = player.Character:FindFirstChildOfClass("Tool")
local bullets = tool.Bullets
local spread = tool.Spread
local firerate = gunInfo[tool.Name]["firerate"]
if tool:GetAttribute("Shooting") == true then player:Kick("You need more power.") return end
if tool:GetAttribute("Reloading") == true then player:Kick("Why?") return end
if bullets.Value < 1 then player:Kick("Your manipulation techniques are atrocious.") return end
if bullets.Value > gunInfo[tool.Name]["maxBullets"] then player:Kick("The underworld calls to you.") return end
local hitPos = initialHitPos
tool:SetAttribute("Shooting", true)
coroutine.wrap(function()
tool.FlashPart.FlashImage.Enabled = true
tool.FlashPart.Light.Enabled = true
task.wait(0.1)
tool.FlashPart.FlashImage.Enabled = false
tool.FlashPart.Light.Enabled = false
end)()
shootingModule.Shoot(tool.FlashPart, player, hitPos, tool)
task.wait(firerate)
if tool.GunType.Value ~= "AUTOMATIC" then
tool:SetAttribute("Shooting", false)
end
end)
module (automatic codeblock only)
AUTOMATIC = function(origin: Part, player: Player, hitPos: Mouse, weapon: Tool)
local firerate = gunInfo[weapon.Name]["firerate"]
weapon:SetAttribute("IsHolding", true)
local updateConnection
updateConnection = updateHitPosEvent.OnServerEvent:Connect(function(updatePlayer, newHitPos)
if updatePlayer == player then
hitPos = newHitPos
end
end)
while weapon:GetAttribute("IsHolding") == true do
if weapon.Bullets.Value < 1 then
weapon:SetAttribute("Shooting", false)
weapon:SetAttribute("IsHolding", false)
updateConnection:Disconnect()
end
Raycasting(origin, player, hitPos, weapon)
weapon.Bullets.Value -= 1
weapon.FlashPart.ShootSound:Play()
if weapon.Bullets.Value < 0 then
weapon.Bullets.Value = 0
end
task.wait(firerate)
end
weapon:SetAttribute("IsHolding", false)
weapon:SetAttribute("Shooting", false)
updateConnection:Disconnect()
end,