Instance is printing on first line but returning nil when trying to index name

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Hi, I’m trying to figure out why there’s an error when I try to get the name of an instance.

  2. What is the issue? Include screenshots / videos if possible!

It prints the instance and its parent (folder named attackable) but says attempt to index nil with name when I’m trying to get its name

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

Tried printing out a bunch of other things but nothing really helped

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local function setPriority(player,number,animID)
print(inQueue[player][2].Parent)
		print(inQueue[player][2].Name)
		
		local enemy
		
		for _,v in pairs(game.Workspace:GetDescendants()) do
			if string.find(v.Name,inQueue[player][2].Name) then
				enemy = v.HumanoidRootPart
			end
		end
		
		print(enemy)
...
![image|406x53](upload://geRsCkEvwJ9N4rmqGN0jGgNFbs8.png)
![image|553x26](upload://6EHcxVy076uethCXmZlWgDfIGzu.png)


--This is where I set the table up
inQueue[plr] = {Time,Enemy.Parent}
	wait()
	setPriority(plr,Time,Enemy) -- set priority is starting the first few lines above

--the table should look something like this
inQueue = {
[playerInstance] = {time,enemy}
}

How looks the function that run this line? inQueue[plr] = {Time,Enemy.Parent}
You sure you want to store in table Enemy.Parent?

Yes, enemy is a humanoid root part so I did .Parent to get the character

Could you elaborate in which line the error occurs? cause I though it was when getting the inQueue[player][2].Name, or its in if string.find(v.Name ?

if string.find(v.Name,inQueue[player][2].Name) then

Its this line specifically the second half

Sorry I was not putting attention and not really reading the code…
This is the issue. You are trying to check all descendants in workspace in order to set enemy variable to the humanoidRootPart found?

		for _,v in pairs(game.Workspace:GetDescendants()) do
			if string.find(v.Name,inQueue[player][2].Name) then
				enemy = v.HumanoidRootPart
			end
		end

You dont need to do that, cause you already have the character stored in the inQueue table.
inQueue[player][2] (which is the model, already contains the HumanoidRootPart, no need to search for it in workspace with that loop)
inQueue[player][2].HumanoidRootPart
so
enemy = inQueue[player][2].HumanoidRootPart
instead of that loop Workspace:GetDescendants()

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.