this code isnt working its supossed to follow the player when it sees him but the ai wont move or do anything
– Define the AI entity
local ai = workspace.Bigfoot
– Set up the pathfinding service
local pathfindingService = game:GetService(“PathfindingService”)
– Set up the player detection parameters
local playerDetectionDistance = 10
local playerDetectionInterval = 1
– Set up the kill distance
local killDistance = 2
– Set up the ignored part
local ignoredPart = “Model”
– Set up the follow part
local followPart = “Handle”
– Set up the AI behavior
local isFollowingPlayer = true
local playerTarget = nil
local path = nil
– Define the function to check if the player is in sight
local function isPlayerInSight(player)
local distance = (player.Character.HumanoidRootPart.Position - ai.HumanoidRootPart.Position).magnitude
if distance <= playerDetectionDistance then
local ray = Ray.new(ai.HumanoidRootPart.Position, (player.Character.HumanoidRootPart.Position - ai.HumanoidRootPart.Position).unit * distance)
local hitPart, hitPosition, hitNormal = workspace:FindPartOnRayWithWhitelist(ray, {player.Character, ignoredPart})
return hitPart and hitPart:IsDescendantOf(player.Character)
end
return false
end
– Define the function to follow the player or a part
local function followTarget(target)
isFollowingPlayer = target:IsA(“Humanoid”)
playerTarget = target
path = pathfindingService:CreatePath({
AgentRadius = 2,
AgentHeight = 5,
AgentCanJump = true,
IgnoreWater = true,
PathPriority = Enum.PathPriority.Lowest,
PathfindingAlgorithm = Enum.PathfindingAlgorithm.Astar
})
path:ComputeAsync(ai.HumanoidRootPart.Position, target.Position)
path:MoveToFinished():Connect(function(status)
if status == Enum.PathStatus.Success then
path:ComputeAsync(ai.HumanoidRootPart.Position, target.Position)
end
end)
end
– Define the function to stop following the player or a part
local function stopFollowingTarget()
isFollowingPlayer = false
playerTarget = nil
path:Cancel()
end
– Define the function to kill the player
local function killPlayer(player)
player.Character:BreakJoints()
end
– Set up the player detection loop
while true do
wait(playerDetectionInterval)
local players = game.Players:GetPlayers()
for i = 1, #players do
local player = players[i]
if isPlayerInSight(player) then
if not isFollowingPlayer then
followTarget(player.Character.Humanoid)
end
if (player.Character.HumanoidRootPart.Position - ai.HumanoidRootPart.Position).magnitude <= killDistance then
killPlayer(player)
end
elseif isFollowingPlayer and player.Character.Humanoid == playerTarget then
stopFollowingTarget()
end
end
if followPart then
if not isFollowingPlayer or followPart.Position ~= playerTarget.RootPart.Position then
followTarget(followPart)
end
elseif not isFollowingPlayer then
stopFollowingTarget()
end
end