--!strict -- Each coin is awarded once, by the server. The remote only describes effects. local Players = game:GetService("Players") local ReplicatedStorage = game:GetService("ReplicatedStorage") local RunService = game:GetService("RunService") local TrophyWallet = require(script.Parent:WaitForChild("TrophyWallet")) local Magnet = require(ReplicatedStorage:WaitForChild("LootMagnet") :: ModuleScript) local Mobility = require(ReplicatedStorage:WaitForChild("ArenaMobility") :: ModuleScript) local Modes = require(ReplicatedStorage:WaitForChild("ArenaModes") :: ModuleScript) local round = ReplicatedStorage:WaitForChild("ArenaRound") local MIN_COINS, MAX_COINS = 10, 20 local MIN_VALUE, MAX_VALUE = 10, 15 local AIR_TIME = .59 local COIN_STAGGER = .065 local LIFE = 16 local remote = Instance.new("RemoteEvent") remote.Name = "TrophyDropFX" remote:SetAttribute("AirTime", AIR_TIME) remote:SetAttribute("MagnetTime", Magnet.Duration) remote.Parent = ReplicatedStorage type Coin = {position: Vector3, amount: number, launchAt: number?, collected: boolean} type Drop = { owner: Player, mode: string, center: Vector3, coins: {Coin}, readyAt: number, expiresAt: number, nextLaunch: number, collected: number, threshold: number, } local drops: {[number]: Drop} = {} local nextId = 0 local random = Random.new() local function ground(at: Vector3, length: number, geometry: RaycastParams): Vector3? local hit = workspace:Raycast(at + Vector3.yAxis * 2, -Vector3.yAxis * length, geometry) return if hit then hit.Position + Vector3.yAxis * 1.12 else nil end local TrophyDrops = {} function TrophyDrops.spawn(owner: Player, origin: Vector3) local now = workspace:GetServerTimeNow() local mode = Modes.of(owner) if owner.Parent ~= Players or not Modes.open(round, mode, now) then return end local geometry = Modes.collision(mode) local center = ground(origin, 180, geometry) if not center then return end local count = random:NextInteger(MIN_COINS, MAX_COINS) local coins: {Coin} = {} local visual = {} local rotation = random:NextNumber(0, math.pi * 2) for i = 1, count do -- Spread across the whole patch; offset the arms so the value labels can breathe. local angle = rotation + i * 2.39996 local dir = Vector3.new(math.cos(angle), 0, math.sin(angle)) local distance = 1.5 + 8.6 * math.sqrt((i - .5) / count) local wall = workspace:Raycast(center, dir * distance, geometry) if wall then distance = math.max(0, wall.Distance - .65) end local position = ground(center + dir * distance, 9, geometry) or center local amount = random:NextInteger(MIN_VALUE, MAX_VALUE) coins[i] = {position = position, amount = amount, launchAt = nil, collected = false} visual[i] = {position = position, amount = amount} end nextId += 1 drops[nextId] = { owner = owner, mode = mode, center = center, coins = coins, readyAt = now + AIR_TIME + .14, expiresAt = now + LIFE, nextLaunch = now, collected = 0, threshold = math.ceil(count * .8), } remote:FireClient(owner, "Drop", nextId, origin, visual, now) end local function checkCoins(id: number, drop: Drop, now: number) local owner = drop.owner local geometry = Modes.collision(drop.mode) local character = owner.Character local humanoid = character and character:FindFirstChildOfClass("Humanoid") local root = character and character.PrimaryPart local position = if root and humanoid and humanoid.Health > 0 then root.Position else nil local radius = if Mobility.active(character, "Magnet", now) then 20 else Magnet.Radius local near = position and (drop.center - position).Magnitude <= radius + 11 local motions = {} local arrivals = {} local total = 0 for index, coin in drop.coins do if coin.collected then continue end local launchAt = coin.launchAt if launchAt then if now < launchAt + Magnet.Duration then continue end coin.collected = true drop.collected += 1 local value = coin.amount local lucky = character and character:GetAttribute("LootLucky") if character and typeof(lucky) == "number" and lucky > 0 then value = math.round(value * 1.25) character:SetAttribute("LootLucky", lucky - 1) end total += value table.insert(arrivals, {index = index, amount = value, milestone = drop.collected == drop.threshold}) elseif position and near then if Magnet.canReach(position, coin.position, radius, geometry) then local start = math.max(now + .06, drop.nextLaunch) coin.launchAt = start drop.nextLaunch = start + COIN_STAGGER table.insert(motions, {index = index, start = start}) end end end if #motions > 0 then remote:FireClient(owner, "Magnet", id, motions) end if total > 0 then TrophyWallet.awardPickup(owner, total) remote:FireClient(owner, "Collected", id, arrivals, owner:GetAttribute("TrophyBalance"), drop.collected, #drop.coins) end if drop.collected == #drop.coins then drops[id] = nil end end local elapsed = 0 RunService.Heartbeat:Connect(function(dt) elapsed += dt if elapsed < .05 then return end elapsed %= .05 if next(drops) == nil then return end local now = workspace:GetServerTimeNow() for id, drop in drops do local owner = drop.owner local connected = owner.Parent == Players if not connected or now >= drop.expiresAt or owner:GetAttribute("InArena") ~= true or Modes.of(owner) ~= drop.mode or not Modes.open(round, drop.mode, now) then drops[id] = nil if connected then remote:FireClient(owner, "Remove", id) end elseif now >= drop.readyAt then checkCoins(id, drop, now) end end end) return TrophyDrops