I tried to make an AI Script for the game that I am working on. I try to print the character name but didn’t work and give me an error message in the output.
Code
local PathfindingService = game:GetService("PathfindingService")
local CollectionService = game:GetService("CollectionService")
local players = game:GetService("Players")
local killer = script.Parent
local humanoid = killer.Humanoid
local rootPart = killer.HumanoidRootPart
killer.PrimaryPart:SetNetworkOwner(nil)
local function RayCast(target)
for index, door in pairs(CollectionService:GetTagged("Door")) do
local origin = rootPart.Position
local direction = (target.HumanoidRootPart.Position - rootPart.Position).unit * math.huge
local ray = Ray.new(origin, direction)
local ignoreList = {killer, door}
local hit, pos = workspace:FindPartOnRayWithIgnoreList(ray, ignoreList)
if hit then
if hit:IsDescendantOf(target) then
return true
end
else
return false
end
end
end
local function IsTarget()
local maxDistance = math.huge
local nearestTarget
for index, player in pairs(players:GetPlayers()) do
if player.Character then
local target = player.Character
local distance = (rootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance < maxDistance and RayCast(target) then
nearestTarget = target
maxDistance = distance
end
end
end
return nearestTarget
end
local function GetPath(targetRoot)
local pathParams = {
["AgentHeight"] = 7,
["AgentRadius"] = 4,
["AgentCanJump"] = false
}
local path = PathfindingService:CreatePath(pathParams)
path:ComputeAsync(rootPart.Position, targetRoot.Position)
return path
end
local function StartChasing()
local target = IsTarget()
print(target.Name)
end
StartChasing()
Did you get a new output? I think maybe the “killer” isn’t sensing anything in the Raycast function. Raycast makes a laser I think, so that means that if the player isn’t in the laser then it will not
could it be your raycast is hitting part of the killer?
if killer is just part of an npc then i would recommend putting all descendants of the npc into the ignorelist
i believe something is going wrong here, maybe try putting print(player) between these two lines and see what prints
also i dont think index is used so you should be able to replace it with _, if you want to
[EDIT] i think you problem is that the character isnt loaded or something so try and implement a way to wait for the character if you can
The function is called only once so when the player loads in and the function detects that it’s not in range it does not run again. So, try putting the function inside a while loop.
if it was working it will return the character which is what you want it to, your problem (to my knowledge) is that you are not calling the find startchasing function after the players character loads
because you only call it once when the script starts it will never find a character
try putting the thing calling it in a loop (like my previous post) and see if that fixes it