Hello, i need a help with my boss AI, i need to make a boss that its head can follow a player by flying/hovering. im pretty new to this, so i don’'t quite understand on how to do advanced scripting
the enemy script i have currently =
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local humanoid = script.Parent
local root = humanoid.Parent.PrimaryPart
local targetDistance = script:GetAttribute("TargetDistance")
local stopDistance = script:GetAttribute("StopDistance")
local damage = script:GetAttribute("Damage")
local attackDistance = script:GetAttribute("AttackDistance")
local attackWait = script:GetAttribute("AttackWait")
local lastAttack = tick()
local animationtrack = humanoid:LoadAnimation(script.Parent.Animation)
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
if character then
local distanceVector = (player.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
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
animationtrack:Play(0)
end
end
end)
i also took this from a video on youtube, thanks for anyone that can help