Idk what’s wrong with my script. Can someone tell my why it’s happening and is there anyways to solve it?
“Chase” script:
local ServerStorage = game:GetService("ServerStorage")
local SimplePath = require(ServerStorage:WaitForChild("NextbotAI"))
local Goal = nil
local Bot = script.Parent
local Path = SimplePath.new(Bot)
function findnearestPlayer(Origin:Vector3,MaxDistance:number)
local Players = game:GetService("Players")
local NearestPlayer,NearestDistance
for i,player in pairs(Players:GetPlayers()) do
local character = player.Character
local distance = player:DistanceFromCharacter(Origin)
if not character or distance > MaxDistance or (NearestDistance and distance >= NearestDistance) then
continue
end
NearestDistance = distance
NearestPlayer = player
end
return NearestPlayer
end
while true do
Goal = findnearestPlayer(Bot.PrimaryPart.Position,math.huge)
if Goal then
Path:Run(Goal.Character.PrimaryPart.Position)
if (Goal.Character.PrimaryPart.Position - Bot.PrimaryPart.Position).Magnitude < 4 then
local Humanoid = Goal.Character:FindFirstChildOfClass("bot")
if Humanoid then
Humanoid:TakeDamage(0) -- keep as 0
end
end
end
task.wait()
end
“Death” script (kill part):
local Copy = script.Parent:Clone()
local NPC = script.Parent
local Humanoid
for i,v in pairs(NPC:GetChildren()) do
if v:IsA('bot') then
Humanoid = v
end
end
script.Parent.Touched:connect(function(hit)
if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
hit.Parent.Humanoid.Health = 0
local character = hit.Parent
local KnockBack = Instance.new("BodyVelocity")
KnockBack.P = math.huge
KnockBack.Parent = character:FindFirstChild("HumanoidRootPart")
KnockBack.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
KnockBack.Velocity = character:FindFirstChild("HumanoidRootPart").CFrame.lookVector * -132
game.Debris:AddItem(KnockBack, 0.5)
else
if hit.Name == "Detector" then
Copy.Parent = NPC.Parent
Copy:MakeJoints()
NPC:Destroy()
end
end
end)