Files
Hunter/scripts/RifleSights.server.luau
T
2026-11-29 15:49:17 +03:00

117 lines
3.6 KiB
Luau

--!strict
-- Script in ServerScriptService.
-- Interchangeable sights of the rifle: ReplicatedStorage.RifleParts.Sights holds one model per
-- sight (Iron, RedDot, Scope), all in grip space (the Handle's frame). The chosen sight is part of
-- the tool itself (Tool.RifleMesh.Sight, welded to the Handle), so every player sees it in third
-- person; the tool's "Sight" attribute names it, and the first-person viewmodel copies it.
-- Client -> server: ReplicatedStorage.RifleSight:FireServer(sightName)
-- The choice is kept on the player (attribute RifleSight) and given to every new rifle, so it
-- survives respawns.
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TOOL_NAME = "SniperRifle"
local DEFAULT_SIGHT = "Iron"
local sights = ReplicatedStorage:WaitForChild("RifleParts"):WaitForChild("Sights")
local remote = ReplicatedStorage:WaitForChild("RifleSight") :: RemoteEvent
local function setSight(tool: Tool, name: string)
local mesh = tool:FindFirstChild("RifleMesh")
local handle = tool:FindFirstChild("Handle")
local model = sights:FindFirstChild(name)
if not mesh or not handle or not handle:IsA("BasePart") or not model then
return
end
if tool:GetAttribute("Sight") == name and mesh:FindFirstChild("Sight") then
return
end
local old = mesh:FindFirstChild("Sight")
if old then
old:Destroy()
end
local sight = Instance.new("Model")
sight.Name = "Sight"
for _, p in model:GetChildren() do
if p:IsA("BasePart") then
local c = p:Clone()
c.CFrame = handle.CFrame * p.CFrame
c.Anchored = false
local weld = Instance.new("WeldConstraint")
weld.Part0 = handle
weld.Part1 = c
weld.Parent = c
c.Parent = sight
end
end
sight.Parent = mesh
tool:SetAttribute("Sight", name)
end
local function chosen(player: Player): string
local name = player:GetAttribute("RifleSight")
return if typeof(name) == "string" and sights:FindFirstChild(name) then name else DEFAULT_SIGHT
end
local function rifles(player: Player): { Tool }
local list = {}
for _, container in { player.Character, player:FindFirstChildOfClass("Backpack") } do
local tool = container and container:FindFirstChild(TOOL_NAME)
if tool and tool:IsA("Tool") then
table.insert(list, tool)
end
end
return list
end
local function watch(player: Player, container: Instance)
container.ChildAdded:Connect(function(child)
if child:IsA("Tool") and child.Name == TOOL_NAME then
setSight(child, chosen(player))
end
end)
for _, tool in rifles(player) do
setSight(tool, chosen(player))
end
end
local lastChange: { [Player]: number } = {}
remote.OnServerEvent:Connect(function(player: Player, name: unknown)
if typeof(name) ~= "string" or not sights:FindFirstChild(name) then
return
end
local now = os.clock()
if now - (lastChange[player] or -math.huge) < 0.25 then
return
end
lastChange[player] = now
player:SetAttribute("RifleSight", name)
for _, tool in rifles(player) do
setSight(tool, name)
end
end)
local function onPlayer(player: Player)
-- Every respawn brings a new character and Backpack; the arena grants the rifle.
player.CharacterAdded:Connect(function(character)
watch(player, character)
end)
player.ChildAdded:Connect(function(child)
if child:IsA("Backpack") then
watch(player, child)
end
end)
for _, container in { player.Character, player:FindFirstChildOfClass("Backpack") } do
if container then
watch(player, container)
end
end
end
Players.PlayerAdded:Connect(onPlayer)
for _, player in Players:GetPlayers() do
onPlayer(player)
end
Players.PlayerRemoving:Connect(function(player)
lastChange[player] = nil
end)