Hello there!
I’m currently done making a script for an enemy that hops slowly towards a player and explodes when near said player or dying. The issues however are that:
- Hackers can take control of the NPC and modify it to their will.
- The formatting is mediocre, especially around the chasing part.
How can I fix the two things above?
--//NPC\\--
local NPC = script.Parent
local RootPart = NPC:WaitForChild("HumanoidRootPart")
local Humanoid = NPC:WaitForChild("Humanoid")
--//Services\\--
local Players = game:GetService("Players")
--//Chasing\\--
--Pathfinding
local function FindTarget()
local players = {}
local Nearest = math.huge
local Target = nil
for _, player in pairs(Players:GetPlayers()) do
local character = player.Character or player.CharacterAdded:Wait()
local Magnitude = (NPC.HumanoidRootPart.Position - character.PrimaryPart.Position).Magnitude
if Magnitude <= Nearest then
table.insert(players,{
Magnitude = Magnitude,
Player = player
})
end
end
for _, Entry in pairs(players) do
local Mag = Entry.Magnitude
local Plr = Entry.Player
if Mag <= Nearest then
Nearest= Mag
Target = Plr
end
end
return Target
end
--Chase
local DeathCoro = coroutine.create(function()
local AlreadyExploded = false
if NPC.Parent == workspace then
NPC.HumanoidRootPart.EmitSound:Play()
repeat
task.wait()
until Humanoid.Health <= 1 and not AlreadyExploded
--Explode
NPC.Head.Transparency = 1
AlreadyExploded = true
NPC.HumanoidRootPart.EmitSound:Stop()
NPC.HumanoidRootPart.Explosion:Play()
local Hits = {}
local Explosion = Instance.new("Explosion",NPC.HumanoidRootPart)
Explosion.DestroyJointRadiusPercent = 0
Explosion.BlastRadius = 10
Explosion.Position = NPC.HumanoidRootPart.Position
Explosion.Hit:Connect(function(OnHit)
local CheckIfPlayer = Players:GetPlayerFromCharacter(OnHit.Parent)
if CheckIfPlayer and not Hits[CheckIfPlayer] then
Hits[CheckIfPlayer] = true
local Hum = CheckIfPlayer.Character:FindFirstChildWhichIsA("Humanoid")
Hum:TakeDamage(300)
task.wait(1)
Hits[CheckIfPlayer] = false
end
end)
game:GetService("Debris"):AddItem(NPC,3)
end
end)
coroutine.resume(DeathCoro)
task.wait(5)
local AboutToExplode = false
while task.wait() and not AboutToExplode do
local Player = FindTarget()
if Player == nil then
return
end
local Plr_Char = Player.Character or Player.CharacterAdded:Wait()
--//Chasing
--Making it so it turns at the player
local Reference = Vector3.new(Plr_Char.PrimaryPart.Position.X, NPC.PrimaryPart.Position.Y, Plr_Char.PrimaryPart.Position.Z)
NPC.HumanoidRootPart.CFrame = CFrame.new(NPC.PrimaryPart.Position,Reference)
--Actually chasing
Humanoid.Jump = true
Humanoid:Move(RootPart.CFrame.LookVector * 30)
if Humanoid:GetState() == Enum.HumanoidStateType.Landed then
Humanoid:Move(Vector3.new(0,0,0))
task.wait(3)
end
--Exploding near a player
local FlashWarnTime = 1
local NearPlayer = coroutine.create(function()
local Mag = (NPC.PrimaryPart.Position - Plr_Char.PrimaryPart.Position).Magnitude
if Mag <= 12 and not AboutToExplode then
AboutToExplode = true
Humanoid:Move(Vector3.new(0,0,0))
local FlashWarn = coroutine.create(function()
RootPart.PointLight.Enabled = true
task.wait(FlashWarnTime)
RootPart.PointLight.Enabled = false
task.wait(FlashWarnTime)
end)
coroutine.resume(FlashWarn)
repeat
RootPart.EmitSound.PlaybackSpeed += 1
FlashWarnTime -= 0.1
task.wait(0.1)
until RootPart.EmitSound.PlaybackSpeed == 5
task.wait(2.5)
Humanoid.Health = 0
end
end)
coroutine.resume(NearPlayer)
end