Error with printing player name

Hello, the lost people,

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()
Output

image

Sorry for my lack english

Giving me Teddy vibes. Anyway, you should actually do an if statement before printing:

local target = IsTarget()
if target then
   print(target.Name)
end

nil means that there is no target that is seen, so we should only print if there IS a target.

1 Like

I tried, but it didn’t print anything

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

No, just the same output if i do if target then function.image

Maybe check the IsTarget() function. Will printing anything there show in the output?

image I tried this but didn’t print anything. I think it didn’t get the player

1 Like

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 have a question(Im asking bc I am stupid). Can you show me how.image

something like
local killerparts = killer.Parent:GetDescendants() – make sure this is the npcs model
lcoal ignoreList = {killerparts, door}

I just want to getplayerimage . My issue is it didnt print the player name

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

if player.Character or player.Character:wait() then
—stuff
end

This might help with the character not being loaded before the script fires

image it didnt print anything.

and I tried this

andimage , it printed my name. But the is target function didnt print anything

ok now try replacing print(player) with print(player.Character) and see what prints

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.

in layman’s terms

while true do
    wait()
    StartChasing()
end

at the bottom instead of just StartChasing()

1 Like

Obviously, because the thing your trying to print doesn’t exist. Your IsTarget() function isn’t returning a player

so I need to make return player.Character or return player?

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