--!strict -- Server-owned poses for hitscan. Parts stay in their live positions. local ShotHistory = {} local FRAME_LIMIT = 14 local TELEPORT_DISTANCE = 11 local zones: {[string]: string} = { Head = "Head", Torso = "Body", UpperTorso = "Body", LowerTorso = "Body", ["Left Arm"] = "Limb", ["Right Arm"] = "Limb", ["Left Leg"] = "Limb", ["Right Leg"] = "Limb", LeftUpperArm = "Limb", LeftLowerArm = "Limb", LeftHand = "Limb", RightUpperArm = "Limb", RightLowerArm = "Limb", RightHand = "Limb", LeftUpperLeg = "Limb", LeftLowerLeg = "Limb", LeftFoot = "Limb", RightUpperLeg = "Limb", RightLowerLeg = "Limb", RightFoot = "Limb", } type Pose = {time: number, eye: Vector3, boxes: {[BasePart]: CFrame}} type Track = {character: Model, poses: {Pose}, newest: number, count: number} export type Hit = {part: BasePart, distance: number, position: Vector3, normal: Vector3, localPoint: Vector3, zone: string} local tracks: {[Player]: Track} = {} local function poseAtIndex(track: Track, age: number): Pose return track.poses[(track.newest - 1 - age) % FRAME_LIMIT + 1] end function ShotHistory.record(player: Player, character: Model, head: BasePart, now: number) local track = tracks[player] if not track or track.character ~= character then track = {character = character, poses = {}, newest = 0, count = 0} tracks[player] = track elseif track.count > 0 and (poseAtIndex(track, 0).eye - head.Position).Magnitude > TELEPORT_DISTANCE then track.newest, track.count = 0, 0 end local index = track.newest % FRAME_LIMIT + 1 local pose = track.poses[index] if not pose then pose = {time = now, eye = head.Position, boxes = {}} track.poses[index] = pose end pose.time, pose.eye = now, head.Position table.clear(pose.boxes) for _, part in character:GetChildren() do if part:IsA("BasePart") and zones[part.Name] then pose.boxes[part] = part.CFrame end end track.newest = index track.count = math.min(track.count + 1, FRAME_LIMIT) end function ShotHistory.clear(player: Player) tracks[player] = nil end local function between(player: Player?, character: Model, time: number): (Pose?, Pose?, number) local track = player and tracks[player] if not track or track.character ~= character or track.count == 0 then return nil, nil, 0 end local newer = poseAtIndex(track, 0) if time >= newer.time then return newer, nil, 0 end for age = 1, track.count - 1 do local older = poseAtIndex(track, age) if time >= older.time then return older, newer, math.clamp((time - older.time) / math.max(newer.time - older.time, .001), 0, 1) end newer = older end return newer, nil, 0 end function ShotHistory.eyeAt(player: Player, character: Model, time: number): Vector3? local older, newer, blend = between(player, character, time) if not older then return nil end return if newer then older.eye:Lerp(newer.eye, blend) else older.eye end local function slab(point: number, direction: number, half: number): (number, number) if math.abs(direction) < 1e-7 then if math.abs(point) > half then return math.huge, -math.huge end return -math.huge, math.huge end local a = (-half - point) / direction local b = (half - point) / direction return math.min(a, b), math.max(a, b) end local function intersect(origin: Vector3, direction: Vector3, box: CFrame, half: Vector3, maximum: number): (number?, Vector3?) local from = box:PointToObjectSpace(origin) local ray = box:VectorToObjectSpace(direction) local x, endX = slab(from.X, ray.X, half.X) local y, endY = slab(from.Y, ray.Y, half.Y) local z, endZ = slab(from.Z, ray.Z, half.Z) local near = math.max(x, y, z, 0) if near > math.min(endX, endY, endZ, maximum) then return nil, nil end local normal = if near == 0 then -ray elseif x == near then Vector3.new(-math.sign(ray.X), 0, 0) elseif y == near then Vector3.new(0, -math.sign(ray.Y), 0) else Vector3.new(0, 0, -math.sign(ray.Z)) return near, box:VectorToWorldSpace(normal) end function ShotHistory.cast(character: Model, player: Player?, time: number, origin: Vector3, direction: Vector3, maximum: number): Hit? local older, newer, weight = between(player, character, time) local eye = if older then older.eye else character:GetPivot().Position if newer then eye = eye:Lerp(newer.eye, weight) end local along = math.clamp((eye - origin):Dot(direction), 0, maximum) if (origin + direction * along - eye).Magnitude > 9 then return nil end local hit: Hit? = nil for _, part in character:GetChildren() do if not part:IsA("BasePart") then continue end local zone = zones[part.Name] if not zone then continue end local box = if older then older.boxes[part] else part.CFrame if not box then continue end local nextBox = newer and newer.boxes[part] if nextBox then box = box:Lerp(nextBox, weight) end local distance, normal = intersect(origin, direction, box, part.Size * .5, maximum) if distance and normal then local position = origin + direction * distance maximum = distance hit = { part = part, distance = distance, position = position, normal = normal, localPoint = box:PointToObjectSpace(position), zone = zone, } end end return hit end return ShotHistory