GetChildren() is not selecting the desired part?

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!
    GetChildren() is printing an other part when it’s suppose to print its desired part
    example:

i have a part that was put in the folder called “1”

example video:

robloxapp-20210609-0158306.wmv (4.9 MB)
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 targetPos = 1
local source = game.Workspace.Map.Roads.Road:GetChildren()
local npc = script.Parent

local Animation = script.Animation

local Hum = npc.AnimationController.Animator
--------
--stat--
--====--
local stat = npc.Stat

print(source[targetPos])

local walkspeed = TweenInfo.new(

	source[targetPos].Position.magnitude *
		stat.Walkspeed.Value / stat.Walkspeed.Value,
	Enum.EasingStyle.Sine,
	Enum.EasingDirection.InOut

)

local health = stat.Health
-----------
--service--
-----------
local TS = game:GetService("TweenService")

local path = {}

----------
--global--
----------
local global_variable1 = nil
local global_variable2 = nil

-------------
--FUNCTIONS--
-------------
function Animate()
	local loadanimation = Hum:LoadAnimation(Animation)
	loadanimation:Play()
	global_variable2 = loadanimation
end

function move()
	--easier vector navigation.
	local x = source[targetPos].Position.X
	local y = source[targetPos].Position.Y
	local z = source[targetPos].Position.Z
	--instead of using {Position = path} you can do this.
	path.Position = Vector3.new(x,y,z)

	local movepos = TS:Create(
		npc.PrimaryPart, 
		walkspeed, 

		{CFrame = 
			CFrame.new(path.Position) * 
			CFrame.Angles(0,math.rad(90),0)
		}
	)

	movepos:Play()
	--sets the global variable to movepos.
	global_variable1 = movepos	
end

Animate()
move()

-- listen for when the NPC arrives at a position 
global_variable1.Completed:Connect(function(didArrive)
	print("W")
	if not didArrive then
		error("did not arrive")
		return
	end
	--select the next target.
	targetPos += 1
	-- check if there are any more targets to move to
	if targetPos <= #source then
		move()
	else
		print("Done moving!")
	end
end)

print(source[targetPos]) is going to print the first object in the table, not the part called 1. Use print(table.find(source, targetPos))

1 Like

Thats because its choosing from the first part of the table which will not return the part you want (unless there is only 1 part)

So i would suggest you doing this:

local source = game.Workspace.Map.Roads.Road:GetChildren()
for i,v in pairs(source) do
        if v.Name == "1" then
                source = v
                break
        end
end

What will this do is that it will run a for loop where it searches for all items in the Road folder/model until it finds a folder named “1” then it will assign the source variable as that folder and then breaks the loop

1 Like

Thats pretty much the same thing

1 Like

It’s printing nil. also for @zaydoudou i dont wanna do i,v pairs but if there’s any other way; can you pls tell me? if not then i’d have to do i,v pairs then. :confused:.

Yes, there is another way. Try doing print(source[table.find(source,"1")].Name)

It’s printing nil and i don’t know why…?
(is there another way other than for i,v)?

Oh wait my bad. I was searching it using a string instead of a part so i can’t use table.find()

I also tried it with targetPos as a number, it still prints nil. maybe i am doing table.find() wrong?

My easiest solution is to use for i,v in pairs

does a numeric getchildren() loop works with this too?
Example code:

for i = 1, #source do
       
end

or will it get the same error as the "print(source[targetPos])?

It doesn’t work since you can’t use it to identify the folder’s name

Ahhhhh
alright, i get it. im just gonna do for i,v pairs >:( this worked before and now it doesnt maybe because the folder before was arranged perfectly.
folder:
1 - first (this is targetted first bcuz it’s called in source[targetPos]
2 - two (now called second.)
but the folder got messed up
new folder:
2 - first(wasn’t suppose to be targetted first.)

1 - second(was suppose to be targetted first).
so i think that sums up what’s happening. i gotta make another function for i,v pairs then.

edit(it’s still in the same position as the old one. my bad i thought the folder was arranged wrong.)

another problem.

for i, target in pairs(source) do
	if target.Name == targetPos then 
		print(target.Name)
		--position = target.Position
	end
end

it’s not printing the target name. why?
edit: I realised the targetpos isn’t a string.

Because its checking if the target’s name is a number.

Try doing if target.Name == tostring(targetPos) then

1 Like

That worked!, now i can finish the zombie targetting tower part.