I am making shotgun logic, and my way of doing it is to fire raycasts, and if they hit anything, put them in a raycasts table.
The client’s raycast table looks really weird though
while the server doesn’t have anything in the table at all:
![]()
Client-side:
local function Fire()
local origin = character.PrimaryPart.Position
local aimPos = mouse.Hit.Position
local direction = (aimPos - origin).Unit
if not gun:GetAttribute('Aiming') then
utility.AnimateViewmodel(gun, viewmodel, 'shoot')
end
PlaySound(gun, 'Shoot')
if gunData.gunType ~= 'shotgun' and not gun:GetAttribute('Aiming') then
--direction = GetHipfireSpread(hipfireSpread, direction)
end
if tracersEnabled and gunData.gunType ~= 'shotgun' then
Tracer(viewmodel, gunData, direction)
end
ApplyRecoil(recoilStrength, kickTime, recoveryTime)
task.spawn(function()
effectParts.FlashPart.FlashImage:Emit(1)
effectParts.FlashPart.Light.Enabled = true
task.wait(0.1)
effectParts.FlashPart.Light.Enabled = false
end)
if gunData.gunType ~= 'shotgun' then
local raycast = Raycast(origin, direction, gunData)
local hitPos
if raycast then
hitPos = raycast.Position
end
shoot:FireServer(origin, aimPos, direction, gunData.maxDistance, hitPos, nil)
else
local shotgunAttributes = gunData.shotgunAttributes
local pellets: number = shotgunAttributes.pellets
local spread: number = shotgunAttributes.spread
local raycasts = {}
for i = 1, pellets do
local pelletDirection = direction
if i > 1 then
local randomX = math.rad(math.random(-spread, spread) / 1)
local randomY = math.rad(math.random(-spread, spread) / 1)
pelletDirection = (CFrame.new(Vector3.zero, direction) * CFrame.Angles(randomX, randomY, 0)).LookVector
end
local raycast = Raycast(origin, pelletDirection, gunData)
if raycast then
table.insert(raycasts, raycast)
local part = utility.CreatePart(Vector3.new(1,1,1), Color3.fromRGB(237, 99, 255), Enum.Material.SmoothPlastic)
part.Position = raycast.Position
dService:AddItem(part, 3)
end
end
print(raycasts)
shoot:FireServer(origin, aimPos, direction, gunData.maxDistance, nil, raycasts)
end
end
Server-side:
local function HandleShotgunShot(player: Player, clientOrigin: Vector3, aimPos: Vector3, distance: number, gun: Tool, gunData, raycasts: {RaycastResult})
if typeof(raycasts) ~= "table" or #raycasts == 0 then
warn("Raycast table is empty or not a table")
warn(raycasts, typeof(raycasts))
return
end
local serverOrigin = player.Character.PrimaryPart.Position
local aimDirection = (aimPos - clientOrigin).Unit
local shotgunAttributes = gunData.shotgunAttributes
local pellets = shotgunAttributes.pellets
local spread = shotgunAttributes.spread
local totalAngle = 0
local validPelletCount = 0
for i, result in raycasts do
print('looping angle')
if not result then
warn(`Raycast {i} has no result`)
continue
end
local pelletDir = (result.Position - clientOrigin).Unit
local angle = GetAngleBetweenVectors(aimDirection, pelletDir)
totalAngle += angle
validPelletCount += 1
end
--if validPelletCount == 0 then
-- warn('There are no valid pellets')
-- return
--end
local averageAngle = totalAngle / validPelletCount
local expectedAverageAngle = math.rad(spread * 0.25)
if averageAngle < expectedAverageAngle then
warn(`[ANTI-CHEAT] Shotgun spread too tight! Avg: {math.deg(averageAngle)}°, Expected: {math.deg(expectedAverageAngle)}°`)
return
end
print(averageAngle, expectedAverageAngle)
local direction = aimDirection
for i, result in raycasts do
print('looping shot')
if not result then
warn(`Result {i} has no result`)
continue
end
local clientRay = utility.RecreateClientRaycast(player.Character, clientOrigin, direction * distance)
if not clientRay then
warn('Failed to recreate client raycast')
return
end
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {player.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
local serverRay = workspace:Raycast(
serverOrigin,
direction,
raycastParams
)
if not validator.AreRaycastsSimilar(clientRay, serverRay) then
warn('Shotgun raycast is not similar')
return
end
local isEnemy, enemyChar, enemyHum = validator.DidRaycastHitEnemy(result)
if not isEnemy then
warn('Raycast didnt hit an enemy')
continue
end
dealer.DealRaycastDamage(result, gun, enemyChar, enemyHum, gunData.bodyshotDamage, gunData.headshotDamage, true, false, player)
end
end
