1299 lines
50 KiB
Luau
1299 lines
50 KiB
Luau
--!strict
|
|
-- First-person viewmodel: while the SniperRifle tool is equipped, ReplicatedStorage.SniperViewmodel
|
|
-- (the rifle) plus two square-section stick arms are drawn locked to the camera, and the
|
|
-- third-person tool is hidden locally. Left click shoots (flash, recoil, bullet hole, then the
|
|
-- bolt is worked), right click toggles aiming down the sights, R reloads, F inspects the weapon
|
|
-- (CS-style), Q changes the sight. Shots and reloads are also sent to
|
|
-- the other players, who see them in third person. Moving widens the view (speed effect) and
|
|
-- makes the weapon lag and dip.
|
|
-- Everything is tuned with this script's attributes (studs / degrees / seconds), live-editable
|
|
-- with the ViewmodelTuner panel (P).
|
|
|
|
local Players = game:GetService("Players")
|
|
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
|
local RunService = game:GetService("RunService")
|
|
local StarterPlayer = game:GetService("StarterPlayer")
|
|
local TweenService = game:GetService("TweenService")
|
|
local UserInputService = game:GetService("UserInputService")
|
|
|
|
local TOOL_NAME = "SniperRifle"
|
|
local INSPECT_KEY = Enum.KeyCode.F
|
|
local RELOAD_KEY = Enum.KeyCode.R
|
|
local SIGHT_KEY = Enum.KeyCode.Q -- next sight: iron -> red dot -> scope
|
|
local SKIN_KEY = Enum.KeyCode.E
|
|
local player = Players.LocalPlayer
|
|
local template = ReplicatedStorage:WaitForChild("SniperViewmodel") :: Model
|
|
local ReloadTimeline = require(ReplicatedStorage:WaitForChild("ReloadTimeline") :: ModuleScript)
|
|
local RifleSpray = require(ReplicatedStorage:WaitForChild("RifleSpray") :: ModuleScript)
|
|
local SniperEffects = require(ReplicatedStorage:WaitForChild("SniperEffects") :: ModuleScript)
|
|
local RifleSkin = require(ReplicatedStorage:WaitForChild("RifleSkin") :: ModuleScript)
|
|
local ArenaMobility = require(ReplicatedStorage:WaitForChild("ArenaMobility") :: ModuleScript)
|
|
local UI = require(ReplicatedStorage:WaitForChild("HunterUI") :: ModuleScript)
|
|
local SkinCatalog = require(ReplicatedStorage:WaitForChild("RifleSkinCatalog") :: ModuleScript)
|
|
local cycleSkin = ReplicatedStorage:WaitForChild("ArenaRound"):WaitForChild("CycleRifleSkin") :: RemoteEvent
|
|
local reloadRemote = ReplicatedStorage:WaitForChild("SniperReload") :: RemoteEvent
|
|
local shotRemote = ReplicatedStorage:WaitForChild("SniperShot") :: RemoteEvent
|
|
local SHOT_RANGE = 1000
|
|
|
|
local DEFAULTS = {
|
|
MovementWalkSpeed = StarterPlayer.CharacterWalkSpeed,
|
|
MovementGravity = workspace.Gravity,
|
|
-- movement feel
|
|
FovBase = 70,
|
|
FovBoost = 17, -- extra field of view at full running speed (the speed effect)
|
|
FovSpeed = 16, -- speed (studs/s) that counts as full speed
|
|
MoveDipY = 0.12, -- the weapon lowers while running...
|
|
MoveBack = 0.1, -- ...comes closer...
|
|
MoveRollDeg = 4, -- ...and tilts
|
|
MoveLag = 0.03, -- lags behind speed changes (studs per stud/s of change)
|
|
FallLag = 0.015, -- rises while falling, dips on a jump (studs per stud/s)
|
|
-- shooting
|
|
MagCapacity = 30,
|
|
FireInterval = 0.1, -- seconds between shots: automatic fire while the button is held (600 rpm)
|
|
RecoilBack = 0, -- kick toward the shooter (0: the weapon stays put)
|
|
RecoilPitchDeg = 0, -- muzzle up
|
|
RecoilRollDeg = 0,
|
|
RecoilReturn = 0.09, -- seconds to settle
|
|
CamKickDeg = 0, -- the view jumps up too
|
|
-- The first four shots are exact. Sustained fire follows a fixed spray.
|
|
ShotZoomPct = 3, -- % the field of view narrows at full spray
|
|
SpreadBase = 0.4, -- start of the spray after the exact shots, in degrees
|
|
SpreadPerShot = 0.3, -- added degrees per spray shot...
|
|
SpreadMax = 2.5, -- ...up to this much on top of the base
|
|
SpreadRecover = 5, -- recovery speed for restarting the burst
|
|
SpreadAim = 0.5, -- spray multiplier while aiming down the sights
|
|
ShotBoltDelay = 0, -- after the shot the right hand works the bolt...
|
|
ShotBoltTime = 0, -- ...for this long (0 = no bolt: automatic rifle)
|
|
ShotBoltTilt = 0.35, -- how far the weapon goes toward reload pose B meanwhile (0..1)
|
|
MuzzleX = 0, -- muzzle end, grip space
|
|
MuzzleY = 0.8,
|
|
MuzzleZ = -4.05,
|
|
Damage = 35, -- per hit in the torso (read by SniperShots on the server)
|
|
HeadshotMultiplier = 2, -- head: 70
|
|
LimbMultiplier = 0.75, -- arms / legs: 26
|
|
FlashSize = 0.6, -- first-person flash; kept small so it does not cover the view
|
|
ShotPreviewT = 0.05, -- moment shown by the tuner's "Держать кадр" on the shot tab (s)
|
|
-- weapon, relative to the camera
|
|
OffsetX = 0.65, -- right of the eye
|
|
OffsetY = -1.5, -- below the eye
|
|
OffsetZ = -0.5, -- in front of the eye (grip position)
|
|
YawDeg = 2, -- turns the muzzle toward the screen centre
|
|
PitchDeg = -1,
|
|
RollDeg = -4,
|
|
SwayAmount = 2.2,
|
|
BobAmount = 0.14,
|
|
-- arms, in the weapon's grip space (origin = pistol grip, -Z = barrel, +Y = up, +X = right).
|
|
-- Each arm: a hand block at Hand X/Y/Z and a stick running from it toward the shoulder.
|
|
-- Left (support) arm: under the handguard
|
|
HandX = -0.3,
|
|
HandY = 0.3,
|
|
HandZ = -1.85,
|
|
ArmYawDeg = -55, -- negative = toward the left
|
|
ArmPitchDeg = 20, -- positive = down
|
|
ArmRollDeg = -4,
|
|
ArmLength = 3.1,
|
|
ArmThickness = 0.5,
|
|
HandLength = 0.5,
|
|
HandThickness = 0.48,
|
|
-- Right (trigger) arm: on the pistol grip
|
|
RHandX = 0.05,
|
|
RHandY = -0.05,
|
|
RHandZ = 0.2,
|
|
RArmYawDeg = 13, -- positive = toward the right
|
|
RArmPitchDeg = 30,
|
|
RArmRollDeg = -1,
|
|
RArmLength = 3.0,
|
|
RArmThickness = 0.5,
|
|
RHandLength = 0.5,
|
|
RHandThickness = 0.48,
|
|
-- inspect pose (camera space): where the right hand (the grip) sits and how the weapon is
|
|
-- turned around it
|
|
InspX = 0.945,
|
|
InspY = -1.226,
|
|
InspZ = -1.818,
|
|
InspYawDeg = 70, -- + = muzzle to the left (shows the left side)
|
|
InspPitchDeg = 0,
|
|
InspRollDeg = -15, -- about the view axis, - = muzzle end up
|
|
InspDriftDeg = 8, -- slow extra turn while the weapon is held out
|
|
InspBlendIn = 0.5, -- seconds
|
|
InspHold = 1.6,
|
|
InspBlendOut = 0.6,
|
|
-- reload (see ReplicatedStorage.ReloadTimeline for the order of moves)
|
|
ReloadTime = 2.6, -- seconds
|
|
-- pose A, magazine change: weapon shift/turn, grip space
|
|
RlX = 0.15,
|
|
RlY = 0.6,
|
|
RlZ = -0.6,
|
|
RlYawDeg = 3,
|
|
RlPitchDeg = 31, -- muzzle up
|
|
RlRollDeg = -20,
|
|
-- pose B, working the bolt: weapon to the left and rolled over
|
|
RlBX = -0.5,
|
|
RlBY = 0.3,
|
|
RlBZ = -0.4,
|
|
RlBYawDeg = 20,
|
|
RlBPitchDeg = 12,
|
|
RlBRollDeg = -45,
|
|
MagX = 0, -- bottom of the magazine, where the left hand grabs it (grip space)
|
|
MagY = 0.05,
|
|
MagZ = -0.9,
|
|
MagDrop = 1.6, -- how far down the old magazine is pulled
|
|
MagSizeX = 0.22, -- the loose magazine the left hand carries
|
|
MagSizeY = 0.7,
|
|
MagSizeZ = 0.4,
|
|
BoltX = 0.3, -- bolt handle, where the right hand grabs it (grip space)
|
|
BoltY = 0.7,
|
|
BoltZ = -0.7,
|
|
BoltLift = 0.12,
|
|
BoltPull = 0.35,
|
|
ReloadPreviewU = 0.33, -- frame shown by the tuner's "Держать кадр" (0..1)
|
|
-- aiming down the sights (right mouse button toggles), shared by all sights
|
|
AimTime = 0.18, -- seconds to bring the sight to the eye
|
|
AimSway = 0, -- inertia / bob / running dip left while aimed (0 = none: the sight stays centred)
|
|
AimSlow = 0.3, -- walk speed lost while aimed (0.3 = 30% slower)
|
|
HipSens = 0.4, -- mouse sensitivity with the rifle in hand, not aimed (aimed: Aim<Sight>Sens)
|
|
-- sound volumes (SniperEffects.sound: VolMaster * Vol<group>; 0 = off)
|
|
VolMaster = 1,
|
|
VolShot = 0.3,
|
|
VolReload = 0.5, -- magazine, bolt, dry fire
|
|
VolHit = 0.35, -- the pop on a hit on a player / bot
|
|
VolKill = 0.3, -- the block-break sound when a player / bot is destroyed
|
|
-- hits on bodies, damage numbers (SniperEffects.blast, DamageNumbers)
|
|
HitFxSize = 1.35, -- size of the fiery blast (0 = off)
|
|
DmgNumSpread = 0.65, -- how far the damage numbers fly to the side
|
|
DmgNumRise = 0.7, -- how far they fly up
|
|
KillPopRise = 0.6, -- how high the kill icon + caption flies (straight up)
|
|
}
|
|
-- Aiming, one set per sight (attributes Aim<Sight>X ... Aim<Sight>Sens):
|
|
-- X/Y/Z, YawDeg/PitchDeg/RollDeg: the grip relative to the camera while aimed (like Offset*), so
|
|
-- that the sight's aiming line runs through the screen centre;
|
|
-- Fov: the camera's field of view while aimed (smaller = more zoom);
|
|
-- Sens: mouse sensitivity while aimed (1 = unchanged).
|
|
-- Starting values come from the models: iron sight line 0.972 above the grip, red dot 0.999,
|
|
-- scope axis 1.096; the eye sits a bit behind the rear part of each sight.
|
|
local AIM_KEYS = { "X", "Y", "Z", "YawDeg", "PitchDeg", "RollDeg", "Fov", "Sens" }
|
|
local AIM_DEFAULTS = {
|
|
Iron = { 0, -0.972, -0.45, 0, 0, 0, 55, 0.2 },
|
|
RedDot = { 0, -0.999, 0.24, 0, 0, 0, 50, 0.3 },
|
|
Scope = { 0, -1.096, -0.48, 0, 0, 0, 20, 0.05 },
|
|
}
|
|
for sight, values in AIM_DEFAULTS do
|
|
for i, key in AIM_KEYS do
|
|
(DEFAULTS :: any)["Aim" .. sight .. key] = values[i]
|
|
end
|
|
end
|
|
local SLEEVE_COLOR = Color3.fromRGB(45, 48, 54)
|
|
local HAND_COLOR = Color3.fromRGB(196, 160, 120)
|
|
local MAG_COLOR = Color3.fromRGB(28, 30, 36)
|
|
|
|
for name, value in DEFAULTS do
|
|
if script:GetAttribute(name) == nil then
|
|
script:SetAttribute(name, value)
|
|
end
|
|
end
|
|
local function attr(name: string): number
|
|
return (script:GetAttribute(name) :: number?) or (DEFAULTS :: any)[name]
|
|
end
|
|
|
|
local viewmodel: Model? = nil
|
|
local rifleFlames: RifleSkin.Effect? = nil
|
|
local equippedTool: Tool? = nil
|
|
local triggerHeld = false -- left mouse button down: automatic fire
|
|
local nextShotAt = 0
|
|
local sway = CFrame.identity
|
|
local bobTime = 0
|
|
local inspectStart: number? = nil
|
|
local inspectExit: {pose: CFrame, weight: number, started: number}? = nil
|
|
local reloadStart: number? = nil
|
|
local reloadLength: number? = nil
|
|
local lastReloadBoost: number? = nil
|
|
-- number of the current reload; bumped to cancel one (unequip, death). Not reloadStart: the
|
|
-- render loop clears that at the end of the animation, maybe before the refill timer runs.
|
|
local reloadSerial = 0
|
|
local refillFor: number? = nil -- the reload that still owes a full magazine
|
|
local ammo = DEFAULTS.MagCapacity
|
|
local lastAmmoGift: number? = nil
|
|
local function reloadSeconds(): number
|
|
return reloadLength or attr("ReloadTime")
|
|
end
|
|
-- The magazine is full the moment a reload ends, whichever notices first: the render loop (end of
|
|
-- the animation) or the timer started with the reload. Once only, and not after a cancel.
|
|
local function finishReload(serial: number)
|
|
if refillFor == serial then
|
|
refillFor = nil
|
|
ammo = attr("MagCapacity")
|
|
SniperEffects.sound("ReloadReady")
|
|
end
|
|
end
|
|
local shotStart: number? = nil
|
|
local lastRootCF: CFrame? = nil -- where the rifle was drawn last frame (muzzle flash)
|
|
local moveK = 0 -- 0..1 how fast we run (smoothed)
|
|
local aiming = false -- aimed while the right mouse button is held (the tuner can toggle it too)
|
|
local aimByMouse = false -- the right mouse button is what holds the aim
|
|
local aimK = 0 -- 0..1 how far the sight is at the eye
|
|
local burstShots = 0
|
|
local lagVelocity = Vector3.zero -- smoothed camera-space velocity, for the lag
|
|
|
|
local function canUseWeapon(): boolean
|
|
local character = player.Character
|
|
local humanoid = character and character:FindFirstChildOfClass("Humanoid")
|
|
return player:GetAttribute("InArena") == true and humanoid ~= nil and humanoid.Health > 0
|
|
end
|
|
|
|
local function burstPause(now: number): number
|
|
return if shotStart then now - shotStart else math.huge
|
|
end
|
|
|
|
local function sprayFor(shot: number, aim: number, steady: number): Vector2
|
|
local scale = math.max(0, 1 + (attr("SpreadAim") - 1) * aim) * steady
|
|
return RifleSpray.offset(shot, attr("SpreadBase"), attr("SpreadPerShot"), attr("SpreadMax")) * scale
|
|
end
|
|
|
|
local function smooth(a: number): number
|
|
a = math.clamp(a, 0, 1)
|
|
return a * a * (3 - 2 * a)
|
|
end
|
|
|
|
-- ---------------------------------------------------------------- inspect
|
|
-- The weapon turns around the right hand, so the grip stays in the palm.
|
|
local function inspectKeyPose(extra: number): CFrame
|
|
local pivot = Vector3.new(attr("RHandX"), attr("RHandY"), attr("RHandZ"))
|
|
return CFrame.new(attr("InspX"), attr("InspY"), attr("InspZ"))
|
|
* CFrame.Angles(0, 0, math.rad(attr("InspRollDeg") - extra * 0.5))
|
|
* CFrame.Angles(0, math.rad(attr("InspYawDeg") + extra), 0)
|
|
* CFrame.Angles(math.rad(attr("InspPitchDeg") + extra * 0.5), 0, 0)
|
|
* CFrame.new(-pivot)
|
|
end
|
|
|
|
-- Returns the inspect pose (camera space) and how strongly it replaces the normal pose (0..1).
|
|
local function inspectPose(): (CFrame, number)
|
|
local exit = inspectExit
|
|
if exit then
|
|
local t = (os.clock() - exit.started) / .12
|
|
if t < 1 then return exit.pose, exit.weight * (1 - smooth(t)) end
|
|
inspectExit = nil
|
|
end
|
|
if script:GetAttribute("InspectPreview") == true then
|
|
return inspectKeyPose(0), 1 -- frozen pose while tuning
|
|
end
|
|
local start = inspectStart
|
|
if not start then
|
|
return CFrame.identity, 0
|
|
end
|
|
local blendIn = math.max(attr("InspBlendIn"), 0.01)
|
|
local hold = math.max(attr("InspHold"), 0)
|
|
local blendOut = math.max(attr("InspBlendOut"), 0.01)
|
|
local t = os.clock() - start
|
|
if t >= blendIn + hold + blendOut then
|
|
inspectStart = nil
|
|
return CFrame.identity, 0
|
|
end
|
|
local drift = attr("InspDriftDeg") * smooth(t / (blendIn + hold))
|
|
local weight = math.min(smooth(t / blendIn), 1 - smooth((t - blendIn - hold) / blendOut))
|
|
return inspectKeyPose(drift), weight
|
|
end
|
|
|
|
-- ---------------------------------------------------------------- reload
|
|
-- Current reload time as a fraction 0..1, or nil when not reloading.
|
|
local function reloadU(): number?
|
|
if script:GetAttribute("ReloadPreview") == true then
|
|
return math.clamp(attr("ReloadPreviewU"), 0, 1) -- frozen frame while tuning
|
|
end
|
|
local start = reloadStart
|
|
if not start then
|
|
return nil
|
|
end
|
|
local u = (os.clock() - start) / math.max(reloadSeconds(), 0.1)
|
|
if u >= 1 then
|
|
reloadStart = nil
|
|
finishReload(reloadSerial)
|
|
return nil
|
|
end
|
|
return u
|
|
end
|
|
|
|
-- One of the weapon's reload poses (prefix "Rl" = A, "RlB" = B) as a shift/turn in grip space.
|
|
-- It turns around the middle of the receiver, so the rifle tilts in place instead of swinging
|
|
-- on the grip.
|
|
local RELOAD_PIVOT = Vector3.new(0, 0.7, -1.0)
|
|
local function reloadPose(prefix: string): CFrame
|
|
return CFrame.new(attr(prefix .. "X"), attr(prefix .. "Y"), attr(prefix .. "Z"))
|
|
* CFrame.new(RELOAD_PIVOT)
|
|
* CFrame.Angles(0, math.rad(attr(prefix .. "YawDeg")), 0)
|
|
* CFrame.Angles(math.rad(attr(prefix .. "PitchDeg")), 0, 0)
|
|
* CFrame.Angles(0, 0, math.rad(attr(prefix .. "RollDeg")))
|
|
* CFrame.new(-RELOAD_PIVOT)
|
|
end
|
|
|
|
-- The weapon's shift/turn at reload time u (grip space).
|
|
local function reloadDelta(u: number): CFrame
|
|
local a, b = ReloadTimeline.weapon(u)
|
|
return CFrame.identity:Lerp(reloadPose("Rl"), a):Lerp(reloadPose("RlB"), b)
|
|
end
|
|
|
|
-- ---------------------------------------------------------------- shot
|
|
-- Seconds since the last shot, or nil when the shot is over.
|
|
local function shotTime(): number?
|
|
if script:GetAttribute("ShotPreview") == true then
|
|
return math.max(attr("ShotPreviewT"), 0) -- frozen moment while tuning
|
|
end
|
|
local start = shotStart
|
|
if not start then
|
|
return nil
|
|
end
|
|
local t = os.clock() - start
|
|
if t > attr("ShotBoltDelay") + attr("ShotBoltTime") and t > attr("RecoilReturn") then
|
|
return nil
|
|
end
|
|
return t
|
|
end
|
|
|
|
-- 0..1: the kick, snapping in fast and settling over RecoilReturn.
|
|
local function recoilAmount(t: number): number
|
|
if t < 0.04 then
|
|
return t / 0.04
|
|
end
|
|
return 1 - ReloadTimeline.smooth((t - 0.04) / math.max(attr("RecoilReturn"), 0.05))
|
|
end
|
|
|
|
-- 0..1 progress of the bolt being worked after the shot, or nil.
|
|
local function shotBolt(t: number?): number?
|
|
if not t or attr("ShotBoltTime") <= 0 then
|
|
return nil -- automatic: no bolt to work
|
|
end
|
|
local a = (t - attr("ShotBoltDelay")) / math.max(attr("ShotBoltTime"), 0.05)
|
|
if a <= 0 or a >= 1 then
|
|
return nil
|
|
end
|
|
return a
|
|
end
|
|
|
|
-- Where a hand is during the reload or the bolt after a shot (grip space), given its normal place.
|
|
local function reloadHand(prefix: string, rest: Vector3, u: number?, boltA: number?): Vector3
|
|
if prefix == "R" and boltA then
|
|
return ReloadTimeline.boltCycle(
|
|
boltA,
|
|
rest,
|
|
Vector3.new(attr("BoltX"), attr("BoltY"), attr("BoltZ")),
|
|
attr("BoltLift"),
|
|
attr("BoltPull")
|
|
)
|
|
end
|
|
if not u then
|
|
return rest
|
|
end
|
|
if prefix == "" then
|
|
return ReloadTimeline.leftHand(u, rest, Vector3.new(attr("MagX"), attr("MagY"), attr("MagZ")), attr("MagDrop"))
|
|
end
|
|
return ReloadTimeline.rightHand(
|
|
u,
|
|
rest,
|
|
Vector3.new(attr("BoltX"), attr("BoltY"), attr("BoltZ")),
|
|
attr("BoltLift"),
|
|
attr("BoltPull")
|
|
)
|
|
end
|
|
|
|
-- Where the loose magazine sits for a left-hand position (grip space): the hand holds its
|
|
-- bottom, the rest goes up toward the magazine well.
|
|
local function magazineAt(hand: Vector3): CFrame
|
|
return CFrame.new(hand + Vector3.new(0, attr("MagSizeY") / 2 - 0.05, 0))
|
|
end
|
|
local function leftHandAt(u: number): Vector3
|
|
local rest = Vector3.new(attr("HandX"), attr("HandY"), attr("HandZ"))
|
|
return reloadHand("", rest, u)
|
|
end
|
|
|
|
local TOSS_VELOCITY = Vector3.new(-6, 6, -1) -- camera space, studs/s: up and out to the left
|
|
local TOSS_GRAVITY = Vector3.new(0, -28, 0)
|
|
|
|
-- Shows the old magazine (pulled out, then thrown) and the new one (carried in).
|
|
-- rootCF: weapon now; offset: the normal weapon offset from the camera.
|
|
local function updateMagazines(vm: Model, rootCF: CFrame, offset: CFrame, cam: Camera, u: number?)
|
|
local oldMag = vm:FindFirstChild("OldMagazine") :: Part?
|
|
local newMag = vm:FindFirstChild("NewMagazine") :: Part?
|
|
if not oldMag or not newMag then
|
|
return
|
|
end
|
|
local size = Vector3.new(attr("MagSizeX"), attr("MagSizeY"), attr("MagSizeZ"))
|
|
oldMag.Size = size
|
|
newMag.Size = size
|
|
local old, flyTime, new = nil, 0, false
|
|
if u then
|
|
old, flyTime, new = ReloadTimeline.magazine(u, reloadSeconds())
|
|
end
|
|
if old == "held" then
|
|
oldMag.CFrame = rootCF * magazineAt(leftHandAt(u :: number))
|
|
elseif old == "flying" then
|
|
-- thrown from where the hand let go, in camera space (independent of sway/bob)
|
|
local r = ReloadTimeline.MAG_RELEASE
|
|
local release = offset * reloadDelta(r) * magazineAt(leftHandAt(r))
|
|
local t = flyTime
|
|
oldMag.CFrame = cam.CFrame
|
|
* CFrame.new(TOSS_VELOCITY * t + TOSS_GRAVITY * (0.5 * t * t))
|
|
* release
|
|
* CFrame.Angles(t * 4, 0, t * 7)
|
|
end
|
|
oldMag.Transparency = if old then 0 else 1
|
|
if new then
|
|
newMag.CFrame = rootCF * magazineAt(leftHandAt(u :: number))
|
|
end
|
|
newMag.Transparency = if new then 0 else 1
|
|
end
|
|
|
|
-- ---------------------------------------------------------------- arms
|
|
local function stickPart(name: string, color: Color3): Part
|
|
local p = Instance.new("Part")
|
|
p.Name = name
|
|
p.Anchored = true
|
|
p.CanCollide = false
|
|
p.CanQuery = false
|
|
p.CanTouch = false
|
|
p.CastShadow = false
|
|
p.Material = Enum.Material.SmoothPlastic
|
|
p.Color = color
|
|
return p
|
|
end
|
|
|
|
-- shoulder = true: the arm's far end is fixed to the body (where it is in the normal pose),
|
|
-- so when the weapon or the hand moves away (inspect, reload) the arm turns and stretches to
|
|
-- follow the hand.
|
|
local ARMS = {
|
|
{ prefix = "", hand = "LeftHandStick", arm = "LeftArmStick", shoulder = true },
|
|
{ prefix = "R", hand = "RightHandStick", arm = "RightArmStick", shoulder = true },
|
|
}
|
|
|
|
-- rootCF: where the weapon is now; restCF: where it would be in the normal pose;
|
|
-- u: reload time (0..1) or nil; boltA: bolt after a shot (0..1) or nil.
|
|
local function updateArms(vm: Model, rootCF: CFrame, restCF: CFrame, u: number?, boltA: number?)
|
|
for _, a in ARMS do
|
|
local hand = vm:FindFirstChild(a.hand) :: Part?
|
|
local arm = vm:FindFirstChild(a.arm) :: Part?
|
|
if hand and arm then
|
|
local p = a.prefix
|
|
local handLen, handT = math.max(attr(p .. "HandLength"), 0.01), math.max(attr(p .. "HandThickness"), 0.01)
|
|
local armLen, armT = math.max(attr(p .. "ArmLength"), 0.01), math.max(attr(p .. "ArmThickness"), 0.01)
|
|
local rest = Vector3.new(attr(p .. "HandX"), attr(p .. "HandY"), attr(p .. "HandZ"))
|
|
local dir = CFrame.Angles(0, math.rad(attr(p .. "ArmYawDeg")), 0)
|
|
* CFrame.Angles(math.rad(attr(p .. "ArmPitchDeg")), 0, 0)
|
|
* CFrame.Angles(0, 0, math.rad(attr(p .. "ArmRollDeg")))
|
|
local local_ = CFrame.new(reloadHand(p, rest, u, boltA)) * dir
|
|
local base = rootCF * local_
|
|
hand.Size = Vector3.new(handT, handT, handLen)
|
|
hand.CFrame = base
|
|
if a.shoulder then
|
|
local wrist = (base * CFrame.new(0, 0, handLen / 2)).Position
|
|
local shoulder = (restCF * CFrame.new(rest) * dir * CFrame.new(0, 0, handLen / 2 + armLen)).Position
|
|
local length = math.max((shoulder - wrist).Magnitude, 0.01)
|
|
arm.Size = Vector3.new(armT, armT, length)
|
|
arm.CFrame = CFrame.lookAt((wrist + shoulder) / 2, shoulder, base.UpVector)
|
|
else
|
|
arm.Size = Vector3.new(armT, armT, armLen)
|
|
arm.CFrame = base * CFrame.new(0, 0, handLen / 2 + armLen / 2)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
-- ---------------------------------------------------------------- frame update
|
|
local function hideTool(tool: Tool)
|
|
for _, d in tool:GetDescendants() do
|
|
if d:IsA("BasePart") then
|
|
d.LocalTransparencyModifier = 1
|
|
end
|
|
end
|
|
end
|
|
|
|
-- ---------------------------------------------------------------- aiming down the sights
|
|
-- The sight being aimed through: the tool's sight, or the one the tuner holds up.
|
|
local function aimSight(): string
|
|
local name: any = nil
|
|
if script:GetAttribute("AimPreview") == true then
|
|
name = script:GetAttribute("AimPreviewSight")
|
|
end
|
|
if name == nil and equippedTool then
|
|
name = equippedTool:GetAttribute("Sight")
|
|
end
|
|
return if typeof(name) == "string" and AIM_DEFAULTS[name] then name else "Iron"
|
|
end
|
|
|
|
-- The grip relative to the camera while aimed through a sight.
|
|
local function aimPose(sight: string): CFrame
|
|
local p = "Aim" .. sight
|
|
return CFrame.new(attr(p .. "X"), attr(p .. "Y"), attr(p .. "Z"))
|
|
* CFrame.Angles(math.rad(attr(p .. "PitchDeg")), math.rad(attr(p .. "YawDeg")), math.rad(attr(p .. "RollDeg")))
|
|
end
|
|
|
|
local AIM_DOT_SIZE = UDim2.fromOffset(5, 5)
|
|
local function makeAimDot(parent: Instance)
|
|
local dot = Instance.new("Frame")
|
|
dot.Name = "AimDot"
|
|
dot.AnchorPoint = Vector2.new(0.5, 0.5)
|
|
dot.Position = UDim2.fromScale(0.5, 0.5)
|
|
dot.Size = AIM_DOT_SIZE
|
|
dot.BackgroundColor3 = Color3.fromRGB(255, 40, 30)
|
|
dot.BorderSizePixel = 0
|
|
dot.ZIndex = 2
|
|
dot.Parent = parent
|
|
Instance.new("UICorner", dot).CornerRadius = UDim.new(0.5, 0)
|
|
end
|
|
|
|
-- Scope picture: at full aim through the scope the rifle is hidden and the screen turns into the
|
|
-- view through the tube: black around a round lens, thin cross hairs and a red centre dot.
|
|
local scopeGui = Instance.new("ScreenGui")
|
|
scopeGui.Name = "ScopeOverlay"
|
|
scopeGui.IgnoreGuiInset = true
|
|
scopeGui.ResetOnSpawn = false
|
|
scopeGui.DisplayOrder = -1 -- under the ammo counter and the tuner
|
|
scopeGui.Enabled = false
|
|
scopeGui.Parent = player:WaitForChild("PlayerGui")
|
|
do
|
|
local lens = Instance.new("Frame")
|
|
lens.Name = "Lens"
|
|
lens.AnchorPoint = Vector2.new(0.5, 0.5)
|
|
lens.Position = UDim2.fromScale(0.5, 0.5)
|
|
lens.Size = UDim2.fromScale(0.92, 0.92)
|
|
lens.BackgroundTransparency = 1
|
|
lens.Parent = scopeGui
|
|
local square = Instance.new("UIAspectRatioConstraint")
|
|
square.DominantAxis = Enum.DominantAxis.Height
|
|
square.Parent = lens
|
|
Instance.new("UICorner", lens).CornerRadius = UDim.new(0.5, 0)
|
|
local outside = Instance.new("UIStroke") -- the black around the lens
|
|
outside.Color = Color3.new(0, 0, 0)
|
|
outside.Thickness = 3000
|
|
outside.Parent = lens
|
|
local rim = Instance.new("Frame") -- soft dark edge inside the lens
|
|
rim.AnchorPoint = Vector2.new(0.5, 0.5)
|
|
rim.Position = UDim2.fromScale(0.5, 0.5)
|
|
rim.Size = UDim2.fromScale(1, 1)
|
|
rim.BackgroundTransparency = 1
|
|
rim.Parent = lens
|
|
Instance.new("UICorner", rim).CornerRadius = UDim.new(0.5, 0)
|
|
local shade = Instance.new("UIStroke")
|
|
shade.Color = Color3.new(0, 0, 0)
|
|
shade.Thickness = 28
|
|
shade.Transparency = 0.55
|
|
shade.ApplyStrokeMode = Enum.ApplyStrokeMode.Border
|
|
shade.Parent = rim
|
|
local function line(size: UDim2)
|
|
local f = Instance.new("Frame")
|
|
f.AnchorPoint = Vector2.new(0.5, 0.5)
|
|
f.Position = UDim2.fromScale(0.5, 0.5)
|
|
f.Size = size
|
|
f.BackgroundColor3 = Color3.new(0, 0, 0)
|
|
f.BorderSizePixel = 0
|
|
f.Parent = lens
|
|
end
|
|
line(UDim2.new(1, 0, 0, 1))
|
|
line(UDim2.new(0, 1, 1, 0))
|
|
-- thicker posts from the edge, stopping short of the centre, like a real reticle
|
|
for _, side in { -1, 1 } do
|
|
local h = Instance.new("Frame")
|
|
h.AnchorPoint = Vector2.new(0.5, 0.5)
|
|
h.Position = UDim2.fromScale(0.5 + side * 0.33, 0.5)
|
|
h.Size = UDim2.new(0.34, 0, 0, 6)
|
|
h.BackgroundColor3 = Color3.new(0, 0, 0)
|
|
h.BorderSizePixel = 0
|
|
h.Parent = lens
|
|
local v = h:Clone()
|
|
v.Position = UDim2.fromScale(0.5, 0.5 + side * 0.33)
|
|
v.Size = UDim2.new(0, 6, 0.34, 0)
|
|
v.Parent = lens
|
|
end
|
|
makeAimDot(lens)
|
|
end
|
|
local redDotGui = Instance.new("ScreenGui")
|
|
redDotGui.Name = "RedDotOverlay"
|
|
redDotGui.IgnoreGuiInset = true
|
|
redDotGui.ResetOnSpawn = false
|
|
redDotGui.DisplayOrder = scopeGui.DisplayOrder
|
|
redDotGui.Enabled = false
|
|
makeAimDot(redDotGui)
|
|
redDotGui.Parent = scopeGui.Parent
|
|
|
|
-- Back to the hip: used when the weapon goes away or the view switches to third person.
|
|
-- Walk speed while aiming uses the server's current base speed.
|
|
-- The client moves its own character, so setting it here is enough.
|
|
local function setAimWalk(k: number)
|
|
local character = player.Character
|
|
local humanoid = character and character:FindFirstChildOfClass("Humanoid")
|
|
if character and humanoid then
|
|
local base = (workspace:GetAttribute("HunterWalkSpeed") :: number?) or StarterPlayer.CharacterWalkSpeed
|
|
local multiplier = ArenaMobility.walkMultiplier(character, workspace:GetServerTimeNow())
|
|
local speed = base * (1 - attr("AimSlow") * k) * multiplier
|
|
if math.abs(humanoid.WalkSpeed - speed) > 0.01 then
|
|
humanoid.WalkSpeed = speed
|
|
end
|
|
end
|
|
end
|
|
|
|
local function resetAim()
|
|
aiming = false
|
|
aimK = 0
|
|
scopeGui.Enabled = false
|
|
redDotGui.Enabled = false
|
|
UserInputService.MouseDeltaSensitivity = 1
|
|
setAimWalk(0)
|
|
if script:GetAttribute("Aiming") then
|
|
script:SetAttribute("Aiming", false) -- the Crosshair script hides itself while aimed
|
|
end
|
|
end
|
|
|
|
local soundU: number? = nil -- reload / bolt time at the last frame, for the sound cues
|
|
local soundBolt: number? = nil
|
|
-- Mouse sensitivity. UserInputService.MouseDeltaSensitivity does not reach the default camera (it
|
|
-- turns by the raw mouse), so the turn the camera made this frame is scaled here instead: the
|
|
-- camera reads its look direction back from CurrentCamera every frame, so a scaled turn stays.
|
|
local lookSens = 1 -- hip / sight sensitivity for this frame (1 = the player's own setting)
|
|
local lastLook: Vector2? = nil -- (pitch, yaw) the camera was left at last frame
|
|
local boostFov = 0
|
|
local function applyLookSens(cam: Camera)
|
|
local cf = cam.CFrame
|
|
local look = cf.LookVector
|
|
local pitch = math.asin(math.clamp(look.Y, -1, 1))
|
|
local yaw = math.atan2(-look.X, -look.Z)
|
|
local last = lastLook
|
|
if last and math.abs(lookSens - 1) > 0.001 then
|
|
local dYaw = (yaw - last.Y + math.pi) % (2 * math.pi) - math.pi -- shortest way round
|
|
pitch = math.clamp(last.X + (pitch - last.X) * lookSens, math.rad(-80), math.rad(80))
|
|
yaw = last.Y + dYaw * lookSens
|
|
cam.CFrame = CFrame.new(cf.Position) * CFrame.fromOrientation(pitch, yaw, 0)
|
|
end
|
|
lastLook = Vector2.new(pitch, yaw)
|
|
end
|
|
|
|
local function render(dt: number)
|
|
local vm = viewmodel
|
|
local tool = equippedTool
|
|
if not vm or not tool then
|
|
lastLook = nil
|
|
return
|
|
end
|
|
local cam = workspace.CurrentCamera
|
|
-- third-person tuning view (ThirdPersonRifle): show the real tool, not the camera viewmodel
|
|
if script:GetAttribute("ThirdPersonView") == true then
|
|
lastLook = nil
|
|
RifleSkin.setMode(rifleFlames, "Hidden")
|
|
vm.Parent = nil
|
|
lastRootCF = nil
|
|
resetAim()
|
|
cam.FieldOfView = attr("FovBase")
|
|
for _, d in tool:GetDescendants() do
|
|
if d:IsA("BasePart") then
|
|
d.LocalTransparencyModifier = 0
|
|
end
|
|
end
|
|
return
|
|
end
|
|
vm.Parent = cam
|
|
hideTool(tool)
|
|
applyLookSens(cam) -- with last frame's sensitivity (set below), before anything reads the camera
|
|
|
|
-- inertia: the rifle lags slightly behind mouse movement
|
|
local delta = UserInputService:GetMouseDelta()
|
|
local k = attr("SwayAmount") * 0.002
|
|
local target = CFrame.Angles(math.clamp(delta.Y * k, -0.08, 0.08), math.clamp(delta.X * k, -0.08, 0.08), 0)
|
|
sway = sway:Lerp(target, math.min(1, dt * 8))
|
|
|
|
-- walking bob
|
|
local character = player.Character
|
|
local humanoid = character and character:FindFirstChildOfClass("Humanoid")
|
|
local speed = humanoid and humanoid.MoveDirection.Magnitude or 0
|
|
bobTime += dt * (speed > 0.1 and 9 or 0)
|
|
local bobA = attr("BobAmount") * math.min(speed, 1)
|
|
local bob = CFrame.new(math.sin(bobTime) * bobA, -math.abs(math.cos(bobTime)) * bobA, 0)
|
|
|
|
-- speed effect: wider view and a lagging, dipping weapon while moving
|
|
local root = character and character:FindFirstChild("HumanoidRootPart")
|
|
local velocity = if root and root:IsA("BasePart") then root.AssemblyLinearVelocity else Vector3.zero
|
|
local localVelocity = cam.CFrame:VectorToObjectSpace(velocity)
|
|
local flatSpeed = Vector3.new(localVelocity.X, 0, localVelocity.Z).Magnitude
|
|
moveK += (math.clamp(flatSpeed / math.max(attr("FovSpeed"), 1), 0, 1) - moveK) * math.min(1, dt * 6)
|
|
lagVelocity = lagVelocity:Lerp(localVelocity, math.min(1, dt * 5))
|
|
local change = localVelocity - lagVelocity -- > 0 while speeding up in that direction
|
|
local lagK = attr("MoveLag")
|
|
local move = CFrame.new(
|
|
0, -- no sideways shift when strafing: the weapon keeps its place across the screen
|
|
-attr("MoveDipY") * moveK + math.clamp(-velocity.Y * attr("FallLag"), -0.35, 0.35),
|
|
attr("MoveBack") * moveK + math.clamp(change.Z * lagK, -0.25, 0.25)
|
|
) * CFrame.Angles(0, 0, math.rad(attr("MoveRollDeg")) * moveK)
|
|
|
|
-- aiming down the sights: not while reloading or inspecting (the tuner's hold overrides that)
|
|
local u = reloadU()
|
|
local sight = aimSight()
|
|
local wantAim = script:GetAttribute("AimPreview") == true or (aiming and u == nil and inspectStart == nil)
|
|
aimK = math.clamp(aimK + (if wantAim then dt else -dt) / math.max(attr("AimTime"), 0.01), 0, 1)
|
|
local aimS = ReloadTimeline.smooth(aimK)
|
|
local calm = 1 - aimS * (1 - attr("AimSway"))
|
|
local boostEnds = character and character:GetAttribute("ArenaBoostUntil")
|
|
local fovTarget = if typeof(boostEnds) == "number" and boostEnds > workspace:GetServerTimeNow() then 6 else 0
|
|
boostFov += (fovTarget - boostFov) * (1 - math.exp(-dt * 10))
|
|
local hipFov = attr("FovBase") + attr("FovBoost") * moveK + boostFov
|
|
cam.FieldOfView = hipFov + (attr("Aim" .. sight .. "Fov") - hipFov) * aimS
|
|
local hipSens = attr("HipSens")
|
|
lookSens = hipSens + (attr("Aim" .. sight .. "Sens") - hipSens) * aimS
|
|
setAimWalk(aimS)
|
|
local scoped = sight == "Scope" and aimS > 0.95
|
|
scopeGui.Enabled = scoped
|
|
redDotGui.Enabled = sight == "RedDot" and aimS > 0.95
|
|
vm.Parent = if scoped then nil else cam -- the view through the tube replaces the rifle
|
|
local aimed = aimS > 0.5
|
|
RifleSkin.setMode(rifleFlames, if scoped then "Hidden" elseif aimed then "Focused" else "Full")
|
|
RifleSkin.animate(rifleFlames, os.clock())
|
|
if (script:GetAttribute("Aiming") == true) ~= aimed then
|
|
script:SetAttribute("Aiming", aimed)
|
|
end
|
|
|
|
-- shot: kick, view jump, then the bolt
|
|
local t = shotTime()
|
|
local kick = if t then recoilAmount(t) else 0
|
|
local boltA = shotBolt(t)
|
|
local interval = attr("FireInterval")
|
|
local recovery = RifleSpray.resetDelay(interval, attr("SpreadRecover"))
|
|
local pause = burstPause(os.clock())
|
|
local nextShot = if pause >= recovery then 1 else burstShots + 1
|
|
local sprayShots = math.max(0, burstShots - RifleSpray.PreciseShots)
|
|
local zoom = math.clamp(sprayShots * attr("SpreadPerShot") / math.max(attr("SpreadMax"), .01), 0, 1)
|
|
local release = 1 - math.clamp((pause - interval) / (recovery - interval), 0, 1)
|
|
local steady = if ArenaMobility.active(character, "Steady", workspace:GetServerTimeNow()) then .70 else 1
|
|
cam.FieldOfView *= 1 - attr("ShotZoomPct") / 100 * zoom * release
|
|
script:SetAttribute("SpreadDeg", sprayFor(nextShot, aimS, steady).Magnitude)
|
|
if kick > 0 then
|
|
-- the camera script sets the camera afresh every frame, so this does not add up
|
|
cam.CFrame *= CFrame.Angles(math.rad(attr("CamKickDeg")) * kick * steady, 0, 0)
|
|
end
|
|
local recoil = CFrame.new(0, 0, attr("RecoilBack") * kick * steady)
|
|
* CFrame.Angles(math.rad(attr("RecoilPitchDeg")) * kick * steady, 0, math.rad(attr("RecoilRollDeg")) * kick * steady)
|
|
|
|
local hipOffset = CFrame.new(attr("OffsetX"), attr("OffsetY"), attr("OffsetZ"))
|
|
* CFrame.Angles(math.rad(attr("PitchDeg")), math.rad(attr("YawDeg")), math.rad(attr("RollDeg")))
|
|
local offset = hipOffset:Lerp(aimPose(sight), aimS)
|
|
local pose, weight = inspectPose()
|
|
local held = offset * recoil
|
|
if u then
|
|
held = held * reloadDelta(u)
|
|
elseif boltA then
|
|
held = held * CFrame.identity:Lerp(reloadPose("RlB"), attr("ShotBoltTilt") * math.sin(boltA * math.pi))
|
|
end
|
|
local body = cam.CFrame
|
|
* CFrame.identity:Lerp(sway, calm)
|
|
* CFrame.identity:Lerp(bob, calm)
|
|
* CFrame.identity:Lerp(move, calm)
|
|
local rootCF = body * held:Lerp(pose, weight)
|
|
vm:PivotTo(rootCF)
|
|
lastRootCF = rootCF
|
|
updateArms(vm, rootCF, body * offset, u, boltA)
|
|
updateMagazines(vm, rootCF, offset, cam, u)
|
|
|
|
-- sounds of the moments passed since the last frame (none while the tuner holds a frame)
|
|
if u and script:GetAttribute("ReloadPreview") ~= true then
|
|
local from = soundU
|
|
SniperEffects.cues(SniperEffects.RELOAD_CUES, if from and from <= u then from else -1, u)
|
|
end
|
|
if boltA and script:GetAttribute("ShotPreview") ~= true then
|
|
local from = soundBolt
|
|
SniperEffects.cues(SniperEffects.BOLT_CUES, if from and from <= boltA then from else 0, boltA)
|
|
end
|
|
soundU, soundBolt = u, boltA
|
|
end
|
|
|
|
-- ---------------------------------------------------------------- sights
|
|
-- The sight sits on the tool (RifleSights on the server, tool attribute "Sight"); the viewmodel
|
|
-- carries a copy of the same model from ReplicatedStorage.RifleParts.Sights, in grip space.
|
|
local SIGHT_ORDER = { "Iron", "RedDot", "Scope" }
|
|
local sightModels = ReplicatedStorage:WaitForChild("RifleParts"):WaitForChild("Sights")
|
|
local sightRemote = ReplicatedStorage:WaitForChild("RifleSight") :: RemoteEvent
|
|
local sightConnection: RBXScriptConnection? = nil
|
|
local skinConnection: RBXScriptConnection? = nil
|
|
|
|
local skinGui = Instance.new("ScreenGui")
|
|
skinGui.Name = "RifleSkinNotice"
|
|
skinGui.DisplayOrder = 25
|
|
skinGui.ResetOnSpawn = false
|
|
|
|
local skinNotice = Instance.new("CanvasGroup")
|
|
skinNotice.Name = "Notice"
|
|
skinNotice.Size = UDim2.fromOffset(222, 41)
|
|
skinNotice.AnchorPoint = Vector2.new(1, 1)
|
|
skinNotice.Position = UDim2.new(1, -20, 1, -124)
|
|
skinNotice.BackgroundTransparency = 1
|
|
skinNotice.Visible = false
|
|
skinNotice.Parent = skinGui
|
|
local skinPlate = UI.plate(skinNotice, "Surface", UDim2.fromScale(0, 0), UDim2.fromScale(1, 1), UI.Outline)
|
|
local skinLightning = UI.contour(skinPlate, UI.Blue)
|
|
local keyLabel = UI.element("Keycap", skinPlate, UDim2.fromOffset(12, 9), nil)
|
|
local skinGlint = UI.element("SkinGlint", skinPlate, UDim2.new(1, -33, 0, 11), nil)
|
|
local skinMark = skinGlint:WaitForChild("Mark") :: Frame
|
|
|
|
local skinName = Instance.new("TextLabel")
|
|
skinName.Name = "Name"
|
|
skinName.Position = UDim2.fromOffset(47, 0)
|
|
skinName.Size = UDim2.new(1, -83, 1, 0)
|
|
skinName.BackgroundTransparency = 1
|
|
skinName.TextColor3 = Color3.fromRGB(245, 248, 255)
|
|
skinName.TextXAlignment = Enum.TextXAlignment.Left
|
|
skinName.FontFace = UI.Display
|
|
skinName.TextSize = 15
|
|
skinName.TextTruncate = Enum.TextTruncate.AtEnd
|
|
skinName.Parent = skinNotice
|
|
skinGui.Parent = player:WaitForChild("PlayerGui")
|
|
local skinScale = Instance.new("UIScale", skinNotice)
|
|
local function fitSkinNotice()
|
|
skinScale.Scale = UI.combatScale(skinGui)
|
|
UI.placeCombat(skinNotice, "Skin")
|
|
end
|
|
skinGui:GetPropertyChangedSignal("AbsoluteSize"):Connect(fitSkinNotice)
|
|
fitSkinNotice()
|
|
|
|
local noticeTween: Tween? = nil
|
|
|
|
local function refreshSkin(vm: Model, tool: Tool)
|
|
local grip = vm.PrimaryPart
|
|
if not grip then return end
|
|
local choice = SkinCatalog.get(tool:GetAttribute("Skin"))
|
|
RifleSkin.destroy(rifleFlames)
|
|
rifleFlames = RifleSkin.apply(vm, grip, choice.id, choice.accent)
|
|
if noticeTween then noticeTween:Cancel() end
|
|
skinName.Text = choice.name
|
|
UI.tintPlate(skinPlate, UI.Outline:Lerp(choice.accent, .24))
|
|
skinLightning:SetAttribute("Accent", choice.accent)
|
|
keyLabel.BackgroundColor3 = UI.Ink
|
|
skinMark.BackgroundColor3 = choice.accent
|
|
skinNotice.GroupTransparency = .44
|
|
skinNotice.Visible = true
|
|
noticeTween = TweenService:Create(skinNotice, TweenInfo.new(.14), {GroupTransparency = 0})
|
|
noticeTween:Play()
|
|
end
|
|
|
|
local function applySight(vm: Model, tool: Tool)
|
|
local old = vm:FindFirstChild("Sight")
|
|
if old then
|
|
old:Destroy()
|
|
end
|
|
local name = tool:GetAttribute("Sight")
|
|
local source = if typeof(name) == "string" then sightModels:FindFirstChild(name) else nil
|
|
if not source or not source:IsA("Model") then
|
|
return
|
|
end
|
|
local sight = source:Clone()
|
|
sight.Name = "Sight"
|
|
sight:PivotTo(vm:GetPivot()) -- the viewmodel's pivot is the grip
|
|
for _, d in sight:GetDescendants() do
|
|
if d:IsA("BasePart") then
|
|
d.CastShadow = false
|
|
end
|
|
end
|
|
if name == "RedDot" then
|
|
local lensAnchor = sight:FindFirstChild("RedDot_RedDot")
|
|
if lensAnchor and lensAnchor:IsA("BasePart") then
|
|
-- The centered overlay supplies the first-person dot at full aim.
|
|
lensAnchor.Transparency = 1
|
|
end
|
|
end
|
|
sight.Parent = vm
|
|
end
|
|
|
|
local function nextSight()
|
|
local tool = equippedTool
|
|
if not tool then
|
|
return
|
|
end
|
|
local current = table.find(SIGHT_ORDER, tool:GetAttribute("Sight") :: any) or 0
|
|
sightRemote:FireServer(SIGHT_ORDER[current % #SIGHT_ORDER + 1])
|
|
end
|
|
|
|
-- ---------------------------------------------------------------- equip / unequip
|
|
local function show(tool: Tool)
|
|
if not canUseWeapon() then return end
|
|
equippedTool = tool
|
|
if not viewmodel then
|
|
local vm = template:Clone()
|
|
for _, d in vm:GetDescendants() do
|
|
if d:IsA("BasePart") then
|
|
d.Anchored = true
|
|
d.CanCollide = false
|
|
d.CanQuery = false
|
|
d.CanTouch = false
|
|
d.CastShadow = false
|
|
end
|
|
end
|
|
for _, a in ARMS do
|
|
stickPart(a.hand, HAND_COLOR).Parent = vm
|
|
stickPart(a.arm, SLEEVE_COLOR).Parent = vm
|
|
end
|
|
for _, name in { "OldMagazine", "NewMagazine" } do
|
|
local mag = stickPart(name, MAG_COLOR)
|
|
mag.Transparency = 1
|
|
mag.Parent = vm
|
|
end
|
|
vm.Parent = workspace.CurrentCamera
|
|
viewmodel = vm
|
|
end
|
|
local vm = viewmodel :: Model
|
|
refreshSkin(vm, tool)
|
|
if skinConnection then skinConnection:Disconnect() end
|
|
skinConnection = tool:GetAttributeChangedSignal("Skin"):Connect(function()
|
|
if viewmodel == vm and equippedTool == tool then refreshSkin(vm, tool) end
|
|
end)
|
|
applySight(vm, tool)
|
|
if sightConnection then
|
|
sightConnection:Disconnect()
|
|
end
|
|
sightConnection = tool:GetAttributeChangedSignal("Sight"):Connect(function()
|
|
if viewmodel == vm and equippedTool == tool then
|
|
applySight(vm, tool)
|
|
end
|
|
end)
|
|
end
|
|
|
|
local function hide(tool: Tool)
|
|
if equippedTool ~= tool then
|
|
return
|
|
end
|
|
equippedTool = nil
|
|
inspectStart = nil
|
|
inspectExit = nil
|
|
reloadStart = nil
|
|
refillFor = nil -- a reload cut short by putting the rifle away does not refill
|
|
shotStart = nil
|
|
burstShots = 0
|
|
if skinConnection then
|
|
skinConnection:Disconnect()
|
|
skinConnection = nil
|
|
end
|
|
if noticeTween then noticeTween:Cancel() end
|
|
skinNotice.Visible = false
|
|
if sightConnection then
|
|
sightConnection:Disconnect()
|
|
sightConnection = nil
|
|
end
|
|
resetAim()
|
|
local camera = workspace.CurrentCamera
|
|
boostFov = 0
|
|
if camera then camera.FieldOfView = attr("FovBase") end
|
|
if viewmodel then
|
|
viewmodel:Destroy()
|
|
viewmodel = nil
|
|
end
|
|
rifleFlames = nil
|
|
for _, d in tool:GetDescendants() do
|
|
if d:IsA("BasePart") then
|
|
d.LocalTransparencyModifier = 0
|
|
end
|
|
end
|
|
end
|
|
|
|
local function clearWeapon()
|
|
triggerHeld = false
|
|
aimByMouse = false
|
|
nextShotAt = 0
|
|
burstShots = 0
|
|
lastRootCF = nil
|
|
local tool = equippedTool
|
|
if tool then hide(tool) end
|
|
end
|
|
|
|
local characterConnections: {RBXScriptConnection} = {}
|
|
local function disconnectCharacter()
|
|
for _, connection in characterConnections do connection:Disconnect() end
|
|
table.clear(characterConnections)
|
|
end
|
|
|
|
local function watch(character: Model)
|
|
local humanoid = character:WaitForChild("Humanoid", 10)
|
|
if not humanoid or not humanoid:IsA("Humanoid") or player.Character ~= character then return end
|
|
disconnectCharacter()
|
|
table.insert(characterConnections, humanoid.Died:Connect(clearWeapon))
|
|
table.insert(characterConnections, character.ChildAdded:Connect(function(child)
|
|
if child:IsA("Tool") and child.Name == TOOL_NAME then
|
|
show(child)
|
|
end
|
|
end))
|
|
table.insert(characterConnections, character.ChildRemoved:Connect(function(child)
|
|
if child:IsA("Tool") and child.Name == TOOL_NAME then
|
|
hide(child)
|
|
end
|
|
end))
|
|
local current = character:FindFirstChild(TOOL_NAME)
|
|
if current and current:IsA("Tool") then
|
|
show(current)
|
|
end
|
|
end
|
|
|
|
player.CharacterAdded:Connect(watch)
|
|
player:GetAttributeChangedSignal("InArena"):Connect(function()
|
|
if player:GetAttribute("InArena") == true then
|
|
ammo = attr("MagCapacity")
|
|
burstShots = 0
|
|
local character = player.Character
|
|
local tool = character and character:FindFirstChild(TOOL_NAME)
|
|
if tool and tool:IsA("Tool") and tool ~= equippedTool then show(tool) end
|
|
else
|
|
clearWeapon()
|
|
end
|
|
end)
|
|
player.CharacterRemoving:Connect(function()
|
|
disconnectCharacter()
|
|
clearWeapon()
|
|
ammo = attr("MagCapacity") -- a new life starts with a full magazine
|
|
end)
|
|
if player.Character then task.spawn(watch, player.Character) end
|
|
|
|
-- ---------------------------------------------------------------- ammo counter
|
|
local ammoGui = Instance.new("ScreenGui")
|
|
ammoGui.Name = "AmmoCounter"
|
|
ammoGui.ResetOnSpawn = false
|
|
ammoGui.DisplayOrder = 23
|
|
ammoGui.Parent = player:WaitForChild("PlayerGui")
|
|
local ammoPlate = UI.plate(ammoGui, "Ammo", UDim2.new(1, -20, 1, -16), UDim2.fromOffset(236, 47), UI.Outline)
|
|
UI.contour(ammoPlate, UI.Blue)
|
|
ammoPlate.AnchorPoint = Vector2.new(1, 1)
|
|
local ammoScale = Instance.new("UIScale", ammoPlate)
|
|
local function fitAmmo()
|
|
ammoScale.Scale = UI.combatScale(ammoGui)
|
|
UI.placeCombat(ammoPlate, "Ammo")
|
|
end
|
|
ammoGui:GetPropertyChangedSignal("AbsoluteSize"):Connect(fitAmmo)
|
|
fitAmmo()
|
|
local ammoIcon = UI.element("Ammo", ammoPlate, UDim2.fromOffset(17, 13), nil)
|
|
Instance.new("UIScale", ammoIcon).Scale = 1.2
|
|
local ammoDivider = UI.frame("Divider", ammoPlate, UDim2.fromOffset(62, 10), UDim2.fromOffset(1, 26), UI.Outline)
|
|
ammoDivider.Rotation = 12
|
|
local ammoLabel = UI.label(ammoPlate, "Rounds", "", UDim2.fromOffset(72, 0), UDim2.new(1, -89, 0, 44), 29, UI.Ink)
|
|
ammoLabel.FontFace = UI.Display
|
|
ammoLabel.TextXAlignment = Enum.TextXAlignment.Right
|
|
local ammoTrack = UI.element("ProgressBar", ammoPlate, UDim2.new(0, 9, 1, -6), UDim2.new(1, -22, 0, 2))
|
|
ammoTrack.Name = "ReloadProgress"
|
|
local ammoFill = ammoTrack:WaitForChild("Fill") :: Frame
|
|
ammoFill.BackgroundColor3 = UI.Blue
|
|
UI.sheen(ammoFill)
|
|
local lastAmmo, lastCapacity = -1, -1
|
|
local function refreshAmmo()
|
|
local character = player.Character
|
|
local gift = character and character:GetAttribute("LootAmmo")
|
|
if typeof(gift) == "number" and gift ~= lastAmmoGift then
|
|
lastAmmoGift = gift
|
|
ammo = attr("MagCapacity")
|
|
reloadStart, refillFor = nil, nil
|
|
end
|
|
ammoGui.Enabled = viewmodel ~= nil
|
|
if not viewmodel then return end
|
|
local capacity = attr("MagCapacity")
|
|
if ammo ~= lastAmmo or capacity ~= lastCapacity then
|
|
lastAmmo, lastCapacity = ammo, capacity
|
|
ammoLabel.Text = string.format("%d / %d", ammo, capacity)
|
|
ammoLabel.TextColor3 = if ammo == 0 then Color3.fromRGB(255, 90, 80) else UI.Ink
|
|
end
|
|
local progress = reloadU()
|
|
ammoTrack.Visible = progress ~= nil
|
|
if progress then ammoFill.Size = UDim2.fromScale(progress, 1) end
|
|
end
|
|
local ammoConnection = RunService.Heartbeat:Connect(refreshAmmo)
|
|
script.Destroying:Once(function() ammoConnection:Disconnect() end)
|
|
|
|
-- ---------------------------------------------------------------- actions
|
|
local function reloading(): boolean
|
|
local start = reloadStart
|
|
return start ~= nil and os.clock() - start < reloadSeconds()
|
|
end
|
|
local function interruptInspectForAim()
|
|
local pose, weight = inspectPose()
|
|
inspectStart = nil
|
|
if script:GetAttribute("InspectPreview") == true then script:SetAttribute("InspectPreview", false) end
|
|
inspectExit = if weight > 0 then {pose = pose, weight = weight, started = os.clock()} else nil
|
|
end
|
|
|
|
local function startInspect()
|
|
if viewmodel and not reloading() and not aimByMouse then
|
|
inspectExit = nil
|
|
inspectStart = os.clock() -- pressing again restarts the inspect
|
|
aiming = false
|
|
end
|
|
end
|
|
local function startReload(force: boolean?)
|
|
if viewmodel and not reloading() and (force or ammo < attr("MagCapacity")) then
|
|
inspectStart = nil -- a reload cancels the inspect...
|
|
inspectExit = nil
|
|
shotStart = nil -- ...and the bolt after a shot (the reload works it anyway)
|
|
burstShots = 0
|
|
aiming = false -- ...and brings the sight down from the eye
|
|
local start = os.clock()
|
|
reloadStart = start
|
|
local character = player.Character
|
|
local charge = character and character:GetAttribute("LootReload")
|
|
reloadLength = attr("ReloadTime")
|
|
if typeof(charge) == "number" and charge ~= lastReloadBoost then
|
|
lastReloadBoost = charge
|
|
reloadLength *= .5
|
|
end
|
|
reloadSerial += 1
|
|
local serial = reloadSerial
|
|
refillFor = serial
|
|
script:SetAttribute("ReloadDuration", reloadLength)
|
|
script:SetAttribute("ReloadStamp", start) -- own character in third person (ThirdPersonRifle)
|
|
reloadRemote:FireServer() -- the other players see it in third person
|
|
task.delay(reloadLength, function()
|
|
finishReload(serial)
|
|
end)
|
|
end
|
|
end
|
|
|
|
local function fire()
|
|
local vm = viewmodel
|
|
if not vm or not canUseWeapon() or reloading() then
|
|
return
|
|
end
|
|
local now = os.clock()
|
|
if now < nextShotAt then
|
|
return -- faster than the rate of fire
|
|
end
|
|
if ammo <= 0 then
|
|
triggerHeld = false -- one click, then the reload; no clicking every frame
|
|
SniperEffects.sound("DryFire")
|
|
startReload()
|
|
return
|
|
end
|
|
-- keep the cadence exact while the button is held (frames do not land on the interval)
|
|
local interval = attr("FireInterval")
|
|
nextShotAt = (if now - nextShotAt < 0.05 then nextShotAt else now) + interval
|
|
burstShots = if burstPause(now) >= RifleSpray.resetDelay(interval, attr("SpreadRecover")) then 1 else burstShots + 1
|
|
ammo -= 1
|
|
inspectStart = nil
|
|
inspectExit = nil
|
|
shotStart = now
|
|
RifleSkin.pulse(rifleFlames)
|
|
SniperEffects.sound("Shot")
|
|
local cam = workspace.CurrentCamera
|
|
local firedAt = workspace:GetServerTimeNow()
|
|
local character = player.Character
|
|
local steady = if ArenaMobility.active(character, "Steady", firedAt) then .70 else 1
|
|
local spray = sprayFor(burstShots, ReloadTimeline.smooth(aimK), steady)
|
|
local direction = (cam.CFrame * CFrame.Angles(math.rad(spray.Y), math.rad(-spray.X), 0)).LookVector
|
|
-- Draw the flash and tracer immediately. Only the server reports an impact.
|
|
local params = RaycastParams.new()
|
|
params.FilterType = Enum.RaycastFilterType.Exclude
|
|
params.FilterDescendantsInstances = { player.Character :: Instance, cam }
|
|
local result = workspace:Raycast(cam.CFrame.Position, direction * SHOT_RANGE, params)
|
|
local predictedHit = if result then result.Position else cam.CFrame.Position + direction * SHOT_RANGE
|
|
local origin = cam.CFrame.Position
|
|
local head = character and character:FindFirstChild("Head")
|
|
if head and head:IsA("BasePart") and (script:GetAttribute("ThirdPersonView") == true or (head.Position - origin).Magnitude > 2.75) then
|
|
origin = head.Position
|
|
local converge = predictedHit - origin
|
|
if converge:Dot(direction) > .1 then direction = converge.Unit end
|
|
end
|
|
local rootCF = lastRootCF
|
|
if rootCF then
|
|
local muzzle = rootCF * CFrame.new(attr("MuzzleX"), attr("MuzzleY"), attr("MuzzleZ"))
|
|
-- aimed, the muzzle sits right on the line of sight: a small flash with nothing thrown forward
|
|
local aimed = aimK > 0.3
|
|
SniperEffects.flash(muzzle, attr("FlashSize"), cam, aimed)
|
|
-- the tracer flies from the rifle to where the bullet went (aimed: it shows up further out,
|
|
-- not over the sight)
|
|
local from = muzzle.Position
|
|
if aimed and (predictedHit - from).Magnitude > 20 then
|
|
from += (predictedHit - from).Unit * 12
|
|
end
|
|
SniperEffects.tracer(from, predictedHit)
|
|
end
|
|
shotRemote:FireServer(direction, origin, firedAt)
|
|
end
|
|
|
|
UserInputService.InputBegan:Connect(function(input, processed)
|
|
if processed or not canUseWeapon() or UserInputService:GetFocusedTextBox() then
|
|
return
|
|
end
|
|
if input.UserInputType == Enum.UserInputType.MouseButton1 then
|
|
triggerHeld = true
|
|
fire()
|
|
elseif input.UserInputType == Enum.UserInputType.MouseButton2 then
|
|
if viewmodel then
|
|
interruptInspectForAim()
|
|
aiming = true -- held, not toggled: InputEnded / Heartbeat below let it go
|
|
aimByMouse = true
|
|
end
|
|
elseif input.KeyCode == INSPECT_KEY then
|
|
startInspect()
|
|
elseif input.KeyCode == SIGHT_KEY then
|
|
nextSight()
|
|
elseif input.KeyCode == SKIN_KEY and equippedTool then
|
|
cycleSkin:FireServer()
|
|
elseif input.KeyCode == Enum.KeyCode.One then
|
|
local character = player.Character
|
|
local humanoid = character and character:FindFirstChildOfClass("Humanoid")
|
|
local backpack = player:FindFirstChildOfClass("Backpack")
|
|
local rifle = backpack and backpack:FindFirstChild(TOOL_NAME)
|
|
if humanoid then
|
|
if equippedTool then
|
|
humanoid:UnequipTools()
|
|
elseif rifle and rifle:IsA("Tool") then
|
|
humanoid:EquipTool(rifle)
|
|
end
|
|
end
|
|
elseif input.KeyCode == RELOAD_KEY then
|
|
startReload()
|
|
end
|
|
end)
|
|
UserInputService.InputEnded:Connect(function(input)
|
|
if input.UserInputType == Enum.UserInputType.MouseButton1 then
|
|
triggerHeld = false
|
|
elseif input.UserInputType == Enum.UserInputType.MouseButton2 and aimByMouse then
|
|
aiming = false
|
|
aimByMouse = false
|
|
end
|
|
end)
|
|
RunService.Heartbeat:Connect(function()
|
|
if aimByMouse then
|
|
if not UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton2) then
|
|
-- the release can be missed (the Roblox menu or another window took it)
|
|
aiming = false
|
|
aimByMouse = false
|
|
elseif viewmodel and not aiming then
|
|
aiming = true -- still held after a reload / inspect: back to the sight
|
|
end
|
|
end
|
|
if triggerHeld then
|
|
-- the release can be missed (the Roblox menu or another window took it): ask the mouse itself
|
|
if viewmodel and UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) then
|
|
fire()
|
|
else
|
|
triggerHeld = false
|
|
end
|
|
end
|
|
end)
|
|
-- the tuner's "Проиграть" buttons (they work even with a full magazine)
|
|
script:GetAttributeChangedSignal("InspectTrigger"):Connect(startInspect)
|
|
script:GetAttributeChangedSignal("ReloadTrigger"):Connect(function()
|
|
startReload(true)
|
|
end)
|
|
script:GetAttributeChangedSignal("ShotTrigger"):Connect(function()
|
|
if ammo <= 0 then
|
|
ammo = attr("MagCapacity")
|
|
end
|
|
fire()
|
|
end)
|
|
-- the tuner's sight tabs: put the tuned sight on the rifle, "▶ Прицелиться" toggles aiming
|
|
local function matchPreviewSight()
|
|
local name = script:GetAttribute("AimPreviewSight")
|
|
local tool = equippedTool
|
|
if tool and typeof(name) == "string" and tool:GetAttribute("Sight") ~= name then
|
|
sightRemote:FireServer(name)
|
|
end
|
|
end
|
|
script:GetAttributeChangedSignal("AimPreviewSight"):Connect(matchPreviewSight)
|
|
script:GetAttributeChangedSignal("AimPreview"):Connect(function()
|
|
if script:GetAttribute("AimPreview") == true then
|
|
matchPreviewSight()
|
|
end
|
|
end)
|
|
script:GetAttributeChangedSignal("AimTrigger"):Connect(function()
|
|
matchPreviewSight()
|
|
if viewmodel then
|
|
aiming = not aiming
|
|
aimByMouse = false -- the tuner's toggle, not the mouse
|
|
end
|
|
end)
|
|
|
|
RunService:BindToRenderStep("SniperViewmodel", Enum.RenderPriority.Camera.Value + 1, render)
|