I got a script that basically finds the nearest player and moves the humanoid in direction of that player. It works fine with a normal dummy, problem is that when i made custom humanoid, its going in the direction of the player but its not facing it.
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local dp = require(ServerScriptService.ItemTables)
local humanoid = script.Parent
local root = humanoid.Parent.PrimaryPart
root:SetNetworkOwner(nil)
local dzwiek = game:GetService("SoundService")
local targetDistance = 20
local stopDistance = 10
local damage = 5
local attackDistance = 10
local attackWait = 2
local lastAttack = tick()
function findNearestPlayer()
local playerlist = Players:GetPlayers()
local nearestPlayer = nil
local distance = nil
local direction = nil
for _, player in pairs(playerlist) do
local character = player.Character or player.CharacterAdded:Wait()
local distanceVector = character.HumanoidRootPart.Position - root.Position
if not nearestPlayer then
nearestPlayer = player
distance = distanceVector.Magnitude
direction = distanceVector.Unit
elseif distanceVector.Magnitude < distance then
nearestPlayer = player
distance = distanceVector.Magnitude
direction = distanceVector.Unit
end
end
return nearestPlayer, distance, direction
end
RunService.Heartbeat:Connect(function()
local nearestPlayer, distance, direction = findNearestPlayer()
if nearestPlayer then
if distance <= targetDistance and distance > stopDistance then
humanoid:Move(direction)
else
humanoid:Move(Vector3.new())
end
if distance <= attackDistance and tick() - lastAttack >=attackWait then
lastAttack = tick()
nearestPlayer.Character.Humanoid.Health -= damage
end
end
end)
This is my script