I am trying to make a module-based missile tracking system, although simple.
I’d like to see if there are any errors in the script, and see how i can improve.
This code is mainly just code from turtorial bashed together.
-- local module = {}
-- Get the Services we need
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
-- Configuration variables
local DAMAGE = 25
local RANGE = 50
local PARENT = nil
local Connection
-- Find a random player target
local function findTarget(RANGE, PARENT)
local players = Players:GetPlayers()
if #players == 0 then return nil end
-- Collect only players in range
local candidates = {}
for _, player in ipairs(players) do
local character = player.Character
local root = character and character:FindFirstChild("HumanoidRootPart")
if root then
local distance = (root.Position - PARENT.Position).Magnitude
if distance <= RANGE then
table.insert(candidates, root)
end
end
end
-- Pick a random one from candidates
if #candidates > 0 then
return candidates[math.random(1, #candidates)]
end
return nil -- no one in range
end
local function Explode(PARENT)
if PARENT then
PARENT:Destroy()
end
local AlreadyHit = {}
local Hitbox = Instance.new("Part")
Hitbox.Position = PARENT.pos
Hitbox.Size = Vector3.new(15,15,15) -- Size of the hitbox
Hitbox.CanCollide = false
Hitbox.Shape = Enum.PartType.Ball
Hitbox.Parent = workspace.Ignore
Hitbox.Anchored = true
Hitbox.Transparency = 1
DS:AddItem(Hitbox,0.1)
for _,Hit in pairs(workspace:GetPartsInPart(Hitbox)) do
local Hum = Hit.Parent:FindFirstChildOfClass("Humanoid")
local Char = Hit.Parent
if Hum and Hum ~= Char.Humanoid and not table.find(AlreadyHit,Hum) then --Find if player hasnt been damaged and not appear in the table
table.insert(AlreadyHit,Hum)
Hum:TakeDamage(50)
end
end
end
return module