--!strict -- Script in ServerScriptService. -- SniperRifle hitscan. The shooter draws the flash, recoil, and tracer immediately. -- The server owns damage and confirms the impact for clients in the shooter's arena. -- The client sends the direction, camera origin, and its synchronized fire time. -- The server bounds the request against its own poses and collision geometry. -- Server -> same arena: SniperShot(character, endPosition, hitNormal?, flesh?) -- (hitNormal only when something was hit; endPosition is the end of the range on a miss; -- flesh = a body was hit: blood puff instead of a bullet hole) -- Damage: opponents in the same arena. By where the bullet -- went: head = Damage * HeadshotMultiplier, torso = Damage, arms / legs = Damage * LimbMultiplier -- (attributes of the SniperViewmodel config script). The shooter is told how much it took: -- Server -> shooter: SniperHit:FireClient(shooter, position, damage, zone, killed, victimName) -- (zone = "Head" / "Body" / "Limb"; their client shows the number, DamageNumbers, and on a kill -- the kill icon, DamageNumbers). Kills also go to the shooter's arena via KillFeed. local Players = game:GetService("Players") local ReplicatedStorage = game:GetService("ReplicatedStorage") local RunService = game:GetService("RunService") local StarterPlayer = game:GetService("StarterPlayer") local TOOL_NAME = "SniperRifle" local RANGE = 1000 local remote = ReplicatedStorage:WaitForChild("SniperShot") :: RemoteEvent local config = StarterPlayer:WaitForChild("StarterPlayerScripts"):WaitForChild("SniperViewmodel") local hitRemote = ReplicatedStorage:WaitForChild("SniperHit") :: RemoteEvent local damageTaken = Instance.new("RemoteEvent") damageTaken.Name = "DamageTaken" damageTaken.Parent = ReplicatedStorage -- KillFeed: killerName, victimName, headshot, killerUserId, victimModel local killFeed: RemoteEvent = (ReplicatedStorage:FindFirstChild("KillFeed") :: RemoteEvent?) or (function() local r = Instance.new("RemoteEvent") r.Name = "KillFeed" r.Parent = ReplicatedStorage return r end)() type Cadence = {fireTime: number, received: number, tokens: number} local cadence: {[Player]: Cadence} = {} local ShotHistory = require(script.Parent:WaitForChild("ShotHistory") :: ModuleScript) local MAX_REWIND = .24 local VIEW_BUFFER = .045 local SAMPLE_STEP = 1 / 30 local round = ReplicatedStorage:WaitForChild("ArenaRound") local ArenaModes = require(ReplicatedStorage:WaitForChild("ArenaModes") :: ModuleScript) local Objectives = require(script.Parent:WaitForChild("ArenaObjectives") :: ModuleScript) local Hunter = ReplicatedStorage:WaitForChild("Hunter") local Stage = require(Hunter:WaitForChild("Stage") :: ModuleScript) local Runtime = script.Parent:WaitForChild("ModeRuntime") local Stats = require(Runtime:WaitForChild("Stats") :: ModuleScript) local RoundPlayer = require(Runtime:WaitForChild("PlayerRoundState") :: ModuleScript) local TrophyDrops = require(script.Parent:WaitForChild("TrophyDrops")) local PowerDrops = require(script.Parent:WaitForChild("PowerUpDrops")) local PowerEffects = require(script.Parent:WaitForChild("PowerUpEffects")) local function inMode(player: Player, mode: string): boolean return player:GetAttribute("InArena") == true and ArenaModes.of(player) == mode end local function broadcast(mode: string, event: RemoteEvent, ...: any) for _, member in Players:GetPlayers() do if inMode(member, mode) then event:FireClient(member, ...) end end end local function recordArenaKill(player: Player, victim: Player, origin: Vector3, mode: string): number? local now = workspace:GetServerTimeNow() if not ArenaModes.open(round, mode, now) or not inMode(player, mode) or not inMode(victim, mode) or not player.Team or not victim.Team or player.Team == victim.Team then return nil end local side = player:GetAttribute("ArenaTeam") if side ~= "Blue" and side ~= "Orange" then return nil end local scoreAttribute = side .. "Kills" local state = ArenaModes.state(round, mode) Stats.add(player, "kills", 1) player:SetAttribute("RoundKills", ((player:GetAttribute("RoundKills") :: number?) or 0) + 1) state:SetAttribute(scoreAttribute, ((state:GetAttribute(scoreAttribute) :: number?) or 0) + 1) local match = Objectives.current(mode) if match then match.kill(player, victim, origin, now) end TrophyDrops.spawn(player, origin) PowerDrops.spawn(origin, nil, mode) local streak = ((player:GetAttribute("StreakKills") :: number?) or 0) + 1 player:SetAttribute("StreakUntil", now + (round:GetAttribute("StreakWindow") :: number)) player:SetAttribute("StreakKills", streak) return streak end local function damageFor(zone: string): number local base = (config:GetAttribute("Damage") :: number?) or 35 if zone == "Head" then return base * ((config:GetAttribute("HeadshotMultiplier") :: number?) or 2) elseif zone == "Limb" then return base * ((config:GetAttribute("LimbMultiplier") :: number?) or 0.75) end return base end local function finiteVector(value: Vector3): boolean local magnitude = value.Magnitude return magnitude == magnitude and magnitude < math.huge end local function headOf(character: Model): BasePart? local head = character:FindFirstChild("Head") return if head and head:IsA("BasePart") then head else nil end local function obstruction(from: Vector3, to: Vector3, params: RaycastParams): RaycastResult? local delta = to - from local distance = delta.Magnitude if distance < .06 then return nil end return workspace:Raycast(from, delta * ((distance - .03) / distance), params) end local function permitShot(player: Player, at: number, received: number): boolean local interval = math.max((config:GetAttribute("FireInterval") :: number?) or .1, .025) local rate = cadence[player] local tokens = if rate then math.min(3, rate.tokens + (received - rate.received) / interval) else 3 -- A reliable remote may deliver a few legal shots together after network jitter. if tokens < 1 or (rate and at - rate.fireTime < interval * .56) then return false end cadence[player] = {fireTime = at, received = received, tokens = tokens - 1} return true end local function validVictim(victim: Player?, player: Player, model: Model, mode: string): boolean if not victim then return false end return victim.Character == model and inMode(victim, mode) and (not victim.Team or victim.Team ~= player.Team) end local function applyHit( player: Player, character: Model, shooter: Humanoid, model: Model, humanoid: Humanoid, victim: Player?, hit: ShotHistory.Hit, origin: Vector3, params: RaycastParams, mode: string ) local now = workspace:GetServerTimeNow() if not Stage.combatAllowed(ArenaModes.state(round, mode)) then return end local eye = headOf(character) if player.Character ~= character or not inMode(player, mode) or shooter.Health <= 0 or not model.Parent or humanoid.Health <= 0 or not hit.part:IsDescendantOf(model) or not validVictim(victim, player, model, mode) or not ArenaModes.open(round, mode, workspace:GetServerTimeNow()) or not eye then return end -- Even a rewind-confirmed hit must respect live cover on both sides. local livePoint = hit.part.CFrame:PointToWorldSpace(hit.localPoint) local cover = obstruction(origin, livePoint, params) or obstruction(eye.Position, livePoint, params) if cover then broadcast(mode, remote, character, cover.Position, cover.Normal, false) return end local shield = victim and victim:GetAttribute("SpawnShield") if typeof(shield) == "number" and shield > now then broadcast(mode, remote, character, hit.position) return end local before = humanoid.Health PowerEffects.damage(model, humanoid, math.floor(damageFor(hit.zone) + .5), workspace:GetServerTimeNow()) local damage = before - humanoid.Health if damage <= 0 then broadcast(mode, remote, character, hit.position) return end if victim then -- Only the victim receives the direction of a shot that actually removed health. damageTaken:FireClient(victim, model, origin - hit.position, damage) local match = Objectives.current(mode) local onHit = match and match.hit if onHit then onHit(victim, workspace:GetServerTimeNow(), humanoid.Health <= 0) end end local name = if victim then victim.DisplayName elseif humanoid.DisplayName ~= "" then humanoid.DisplayName else model.Name local killed = humanoid.Health <= 0 local root = model.PrimaryPart local streak = if killed and victim then recordArenaKill(player, victim, if root then root.Position else hit.position, mode) else nil hitRemote:FireClient(player, hit.position, math.max(1, math.round(damage)), hit.zone, killed, name, streak) if killed then humanoid.HealthDisplayType = Enum.HumanoidHealthDisplayType.AlwaysOff humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None broadcast(mode, killFeed, player.Name, if victim then victim.Name else name, hit.zone == "Head", player.UserId, model) end broadcast(mode, remote, character, hit.position, hit.normal, true) end remote.OnServerEvent:Connect(function(player: Player, direction: unknown, cameraOrigin: unknown, timestamp: unknown) if typeof(direction) ~= "Vector3" or typeof(cameraOrigin) ~= "Vector3" or typeof(timestamp) ~= "number" then return end if not finiteVector(direction) or not finiteVector(cameraOrigin) or direction.Magnitude < .5 or direction.Magnitude > 1.5 or timestamp ~= timestamp or math.abs(timestamp) == math.huge then return end local received = workspace:GetServerTimeNow() local mode = ArenaModes.of(player) if player:GetAttribute("InArena") ~= true or not ArenaModes.open(round, mode, received) or not Stage.combatAllowed(ArenaModes.state(round, mode)) then return end local oneWay = math.clamp(player:GetNetworkPing() * .5, 0, .20) local ageLimit = math.clamp(oneWay + .10, .12, .30) if timestamp > received + .035 or timestamp < received - ageLimit then return end local shotTime = math.min(timestamp, received) local character = player.Character local shooter = character and character:FindFirstChildOfClass("Humanoid") if not character or not shooter or shooter.Health <= 0 then return end local tool = character:FindFirstChild(TOOL_NAME) local head = headOf(character) if not tool or not tool:IsA("Tool") or not head then return end local eye = head.Position if (cameraOrigin - eye).Magnitude > 3.1 or not permitShot(player, shotTime, received) then return end local members = Players:GetPlayers() local excluded: {Instance} = {} for _, member in members do local current = member.Character if current then table.insert(excluded, current) end end local world = RaycastParams.new() world.FilterType = Enum.RaycastFilterType.Exclude world.FilterDescendantsInstances = excluded world.RespectCanCollide = true if obstruction(eye, cameraOrigin, world) then return end if player:GetAttribute("SpawnShield") ~= nil then RoundPlayer.clearShield(player) end local origin, dir = cameraOrigin, direction.Unit local worldHit = workspace:Raycast(origin, dir * RANGE, world) local remaining = if worldHit then math.max(0, worldHit.Distance - .025) else RANGE local targetTime = math.max(received - MAX_REWIND, shotTime - oneWay - VIEW_BUFFER) local model: Model? = nil local humanoid: Humanoid? = nil local victim: Player? = nil local found: ShotHistory.Hit? = nil local function consider(candidate: Model, owner: Player?) if candidate == character then return end local health = candidate:FindFirstChildOfClass("Humanoid") if not health or health.Health <= 0 then return end local result = ShotHistory.cast(candidate, owner, targetTime, origin, dir, remaining) if result then model, humanoid, victim, found = candidate, health, owner, result remaining = result.distance end end for _, member in members do local candidate = member.Character if candidate and inMode(member, mode) then consider(candidate, member) end end local hit, target, health, owner = found, model, humanoid, victim if not hit or not target or not health then if worldHit then broadcast(mode, remote, character, worldHit.Position, worldHit.Normal, false) else broadcast(mode, remote, character, origin + dir * RANGE) end return end if not validVictim(owner, player, target, mode) then broadcast(mode, remote, character, hit.position) return end local cover = obstruction(eye, hit.position, world) if cover then broadcast(mode, remote, character, cover.Position, cover.Normal, false) return end if owner then local targetHead = headOf(target) local delivery = math.clamp(owner:GetNetworkPing() * .5 + VIEW_BUFFER, VIEW_BUFFER, .14) local previousEye = ShotHistory.eyeAt(player, character, received - delivery) if targetHead and previousEye and obstruction(targetHead.Position, previousEye, world) then -- The defender can still be seeing the previous, occluded shooter pose. task.delay(delivery, applyHit, player, character, shooter, target, health, owner, hit, origin, world, mode) return end end applyHit(player, character, shooter, target, health, owner, hit, origin, world, mode) end) local elapsed = 0 local tracking = RunService.Heartbeat:Connect(function(dt) elapsed += dt if elapsed < SAMPLE_STEP then return end elapsed %= SAMPLE_STEP local now = workspace:GetServerTimeNow() for _, player in Players:GetPlayers() do local character = player.Character local head = character and headOf(character) local humanoid = character and character:FindFirstChildOfClass("Humanoid") if character and head and humanoid and humanoid.Health > 0 and player:GetAttribute("InArena") == true then ShotHistory.record(player, character, head, now) else ShotHistory.clear(player) end end end) script.Destroying:Once(function() tracking:Disconnect() end) Players.PlayerRemoving:Connect(function(player) cadence[player] = nil ShotHistory.clear(player) end)