How to keep shotgun spread on the server

Pellet directions or a random seed from the client let every pellet hit the same spot. How to build the spread on the server from one aim direction.

warningLuaudit check: client-chosen-spread

What it looks like

A shotgun, a spread spell or an inaccurate weapon fires several rays, or one ray with random spread. The server raycasts from the right place, but each ray's direction comes from the client, or the server makes the spread from a random seed the client sends. Nothing compares the rays with the pattern the weapon is meant to fire.

Luau
remote.OnServerEvent:Connect(function(player, directions)
	for index = 1, pellets do
		castPellet(player, directions[index])
	end
end)

What an exploiter could do

Every pellet lands on the same spot, so a shotgun does its full close-range damage at the end of its range, and a weapon balanced around missing never misses. Inaccurate weapons become the strongest in the game.

How to fix it

  • Take one aim direction from the client and build every pellet's direction on the server from the weapon's spread.
  • For random spread, use a Random the server creates and seeds itself. For a fixed pattern, work it out on the server and compare the client's rays with it, as the laser tag tutorial does.
  • Keep the pellet count and spread angle in server-side settings for each weapon.

Vulnerable example

Vulnerable
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Workspace = game:GetService("Workspace")

local range = 70
local pellets = 6
local pelletDamage = 14
local spread = math.rad(6)
local cooldown = 1
local lastShot: { [Player]: number } = {}

ReplicatedStorage.Remotes.FireScattergun.OnServerEvent:Connect(function(player: Player, aim: unknown, seed: unknown)
	if typeof(aim) ~= "Vector3" or not (aim.Magnitude > 0 and aim.Magnitude < math.huge) then
		return
	end
	if typeof(seed) ~= "number" or seed % 1 ~= 0 then
		return
	end
	local character = player.Character
	local head = character and character:FindFirstChild("Head")
	if not character or not head or not head:IsA("BasePart") or not character:FindFirstChild("Scattergun") then
		return
	end
	local now = os.clock()
	if now - (lastShot[player] or -math.huge) < cooldown then
		return
	end
	lastShot[player] = now
	local params = RaycastParams.new()
	params.FilterType = Enum.RaycastFilterType.Exclude
	params.FilterDescendantsInstances = { character }
	local random = Random.new(seed)
	local aimFrame = CFrame.lookAt(Vector3.zero, aim.Unit)
	for _ = 1, pellets do
		local direction = (aimFrame * CFrame.Angles(random:NextNumber(-spread, spread), random:NextNumber(-spread, spread), 0)).LookVector
		local result = Workspace:Raycast(head.Position, direction * range, params)
		local victim = result and result.Instance:FindFirstAncestorOfClass("Model")
		local humanoid = victim and victim:FindFirstChildOfClass("Humanoid")
		if humanoid and humanoid.Health > 0 then
			humanoid:TakeDamage(pelletDamage)
		end
	end
end)

Players.PlayerRemoving:Connect(function(player)
	lastShot[player] = nil
end)

Fixed example

Fixed
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Workspace = game:GetService("Workspace")

local range = 70
local pellets = 6
local pelletDamage = 14
local spread = math.rad(6)
local cooldown = 1
local lastShot: { [Player]: number } = {}

ReplicatedStorage.Remotes.FireScattergun.OnServerEvent:Connect(function(player: Player, aim: unknown)
	if typeof(aim) ~= "Vector3" or not (aim.Magnitude > 0 and aim.Magnitude < math.huge) then
		return
	end
	local character = player.Character
	local head = character and character:FindFirstChild("Head")
	if not character or not head or not head:IsA("BasePart") or not character:FindFirstChild("Scattergun") then
		return
	end
	local now = os.clock()
	if now - (lastShot[player] or -math.huge) < cooldown then
		return
	end
	lastShot[player] = now
	local params = RaycastParams.new()
	params.FilterType = Enum.RaycastFilterType.Exclude
	params.FilterDescendantsInstances = { character }
	local aimFrame = CFrame.lookAt(Vector3.zero, aim.Unit)
	for index = 1, pellets do
		local angle = -spread + (index - 1) * (2 * spread / (pellets - 1))
		local direction = (aimFrame * CFrame.Angles(0, angle, 0)).LookVector
		local result = Workspace:Raycast(head.Position, direction * range, params)
		local victim = result and result.Instance:FindFirstAncestorOfClass("Model")
		local humanoid = victim and victim:FindFirstChildOfClass("Humanoid")
		if humanoid and humanoid.Health > 0 then
			humanoid:TakeDamage(pelletDamage)
		end
	end
end)

Players.PlayerRemoving:Connect(function(player)
	lastShot[player] = nil
end)

How to spot this in your own scripts, and the cases that only look like it, are part of the Luaudit check your AI runs.