I am trying to achieve a Module-Based Tracking Missile, whereas if a player is found, that player is going to be a paste on the floor.
When I fire a rocket, instead of actually moving towards the designated target, its mereley just a frozen brick.
I have tried changing velocities, and including raycast functions, but all have ultimetley made the situation worse.
Very messy, isn’t it?
It’s mainly just code bashed together.
-- Module Below
local ExplosiveModule = {}
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local Debris = game:GetService("Debris")
local DAMAGE = 67
local RANGE = 67
local Parent
local Connection = {}
local Gravity = Vector3.new(0,-workspace.Gravity,0) * .01
function ExplosiveModule.findTarget(RANGE, Parent)
local players = Players:GetPlayers()
if #players == 0 then return nil end
local candidates = {}
for _, player in 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
if #candidates > 0 then
return candidates[math.random(1, #candidates)]
end
return
end
function ExplosiveModule.Explode(Parent)
local Conn = Connection[Parent]
if Conn then
Conn:Disconnect()
Conn.Parent = nil
end
Parent:Destroy()
local AlreadyHit = {}
local Hitbox = Instance.new("Part")
Hitbox.CFrame = Parent.CFrame
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
Debris:AddItem(Hitbox, 1)
for _,Hit in 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
table.insert(AlreadyHit,Hum)
Hum:TakeDamage(50)
end
end
end
function ExplosiveModule.Movement(Velocity, Parent, MousePos, Speed, HomingStrength)
Connection = RunService.Heartbeat:Connect(function(deltaTime)
local Pos = Parent.Position
local TargetDirection = (MousePos - Pos).Unit
Velocity = Velocity:Lerp(TargetDirection * Velocity.Magnitude * Speed, HomingStrength * deltaTime)
local StepVelocity = Velocity + Gravity * deltaTime
local StepDisplacement = (Velocity + StepVelocity) / 2 * deltaTime
local NextPosition = Pos + StepDisplacement
local Direction = NextPosition - Pos
Parent.Position = NextPosition
if Parent.Position.Y <= 0 then
if Connection[Parent] then
Connection[Parent]:Disconnect()
Connection[Parent] = nil
end
end
end)
end
function ExplosiveModule.TargetFound(Parent, target, startTime, lifeTime, Speed, hitDistance)
local isTargeting = false
local destroyConnection
Connection:Disconnect()
destroyConnection = RunService.Heartbeat:Connect(function(deltaTime)
if isTargeting then
return
end
isTargeting = true
task.spawn(function()
if not target or not target.Parent or not target.Parent:FindFirstChild("Humanoid") or target.Parent.Humanoid.Health <= 0 then
target = ExplosiveModule.findTarget(RANGE, Parent)
if not target then
isTargeting = false
task.delay(5, function()
isTargeting = false
end)
return
end
end
local direction = (target.Position - Parent.Position).Unit
Parent.CFrame = CFrame.new(Parent.Position, target.Position)
Parent.Position = Parent.Position + direction * Speed * deltaTime
if (Parent.Position - target.Position).Magnitude < hitDistance then
if target.Parent:FindFirstChild("Humanoid") then
ExplosiveModule.Explode(Parent)
Parent:Destroy()
destroyConnection:Disconnect()
return
end
end
if tick() - startTime > lifeTime then
Parent:Destroy()
destroyConnection:Disconnect()
return
end
isTargeting = false
end)
end)
end
return ExplosiveModule
---Server Below
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Workspace = game:GetService("Workspace")
local ServerStorage = game:GetService("ServerStorage")
local Players = game:GetService("Players")
local Remotes = ReplicatedStorage:FindFirstChild("Remotes")
local WeaponaryRemote = Remotes:FindFirstChild("WeaponaryRemote")
local Modules = ReplicatedStorage:FindFirstChild("Modules")
local ExplosiveModule = require(Modules:FindFirstChild("ExplosiveModule"))
local WarheadTemplate = ServerStorage:FindFirstChild("WarheadTemplate")
if not WarheadTemplate then
warn("WarheadTemplate not found in ServerStorage!")
return
end
local Damage = 75
local Range = 30
local Speed = 15
local hitDistance = 5
local HomingStrength = 1
local Parent
local Connection
WeaponaryRemote.OnServerEvent:Connect(function(player, MousePos)
if typeof(MousePos) ~= "Vector3" then return end
if not player or not player.Character then return end
local tool = player.Character:FindFirstChild("PANZERFAUST M3") or (player.Backpack and player.Backpack:FindFirstChild("PANZERFAUST M3"))
if not tool then return end
local Origin = tool:FindFirstChild("Base") and tool.Base:FindFirstChild("Origin")
if not Origin then return end
local Warhead = WarheadTemplate
Warhead = Warhead:Clone()
Warhead.Parent = Workspace
Warhead.CFrame = Origin.CFrame
Warhead.Anchored = true
Warhead.CanCollide = true
local target = ExplosiveModule.findTarget(Range, Warhead)
if target then
ExplosiveModule.TargetFound(Warhead, target, tick(), 10, Speed, hitDistance)
else
local Direction = MousePos - Warhead.Position
local Velocity = (Direction.Magnitude > 0 and Direction.Unit or Vector3.new(0,0,1)) * Speed
ExplosiveModule.Movement(Velocity, Warhead, MousePos, Speed, HomingStrength)
end
task.delay(10, function()
if Warhead and Warhead.Parent then
ExplosiveModule.Explode(Warhead)
end
end)
end)```
```lua
--Client Below
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local plr = game.Players.LocalPlayer
local Char = plr.Character
local Humanoid = plr.Character.Humanoid
local Mouse = plr:GetMouse()
local tool = script.Parent
local Remotes = ReplicatedStorage.Remotes.WeaponaryRemote
tool.Activated:Connect(function()
local MousePos = Mouse.Hit.Position
Remotes:FireServer(MousePos)
end)```
