I’ve tried many ways and searched for many posts but cant seem to find a solution, is there even a way?:
local PFS = game:GetService("PathfindingService")
for _, part in pairs(script.Parent:GetDescendants()) do
if part:IsA("BasePart") then
part:SetNetworkOwner(nil)
end
end
local randomUserID = math.random(1000000, 100000000)
local playerName = game.Players:GetNameFromUserIdAsync(randomUserID)
local humanoidDescription = game.Players:GetHumanoidDescriptionFromUserId(randomUserID)
script.Parent.Humanoid:ApplyDescription(humanoidDescription)
script.Parent.Name = playerName
local function onHit(hit)
if hit.Parent:FindFirstChildOfClass("Humanoid") and hit.Parent.Name ~= script.Parent.Creator.Value and not hit.Parent:FindFirstChild("EVIL_FLOWER") then
hit.Parent:FindFirstChildOfClass("Humanoid"):TakeDamage(100)
end
end
local function getNearestPlayer()
local lastDist = 10000000
local currentPlayer = nil
for _, player in pairs(game.Players:GetPlayers()) do
if player.Character and player.Character:FindFirstChild("HumanoidRootPart") and player.Name ~= script.Parent.Creator.Value then
local distance = (script.Parent.HumanoidRootPart.Position - player.Character.HumanoidRootPart.Position).Magnitude
if distance < lastDist then
lastDist = distance
currentPlayer = player.Character
end
end
end
return lastDist, currentPlayer
end
local function getPath(destination)
local path = PFS:CreatePath()
path:ComputeAsync(script.Parent.HumanoidRootPart.Position, destination)
return path
end
local function pathFindTo(destination, target)
local path = getPath(destination)
if target and target.Humanoid.Health > 0 then
for i,waypoint in pairs(path:GetWaypoints()) do
if waypoint.Action == Enum.PathWaypointAction.Jump then
script.Parent.Humanoid.Jump = true
end
script.Parent.Humanoid:MoveTo(waypoint.Position)
script.Parent.Humanoid.MoveToFinished:Wait()
end
end
end
while task.wait() do
local dist, player = getNearestPlayer()
spawn(function()
if dist and player then
script.Parent.HumanoidRootPart.CFrame = CFrame.lookAt(script.Parent.HumanoidRootPart.Position, player.HumanoidRootPart.Position)
end
end)
spawn(function()
if dist and player then
pathFindTo(player.HumanoidRootPart.Position, player)
end
end)
end