Help in a npc move to parts randomly

  1. What do you want to achieve? a npc that always move to a random part that all the time moves to a random part inside the folder MoveParts

  2. What is the issue?

local Children = game.Workspace:WaitForChild("EasterEggs"):WaitForChild("PersonajeSecreto"):WaitForChild("MoveParts")

for i, v in pairs(Children) do
	if i == math.random(1, #Children) then
		script.Parent:WaitForChild("Humanoid"):MoveTo(v.Position)
	end
end
  1. What solutions have you tried so far? contact a friend develeoper and he cant solve it
1 Like

Children in this case needs to be a table of parts

You are currently just getting “MoveParts” when you need to get “MoveParts” children

local Children = game.Workspace:WaitForChild("EasterEggs"):WaitForChild("PersonajeSecreto"):WaitForChild("MoveParts"):GetChildren()

You’re forgetting to get the children via :GetChildren() at the end of the first line, but this is not really a good way to do random movement as there can be a chance that it moves to multiple parts, and it does it once, you can probably have an infinite while loop to move the npc constantly by moving them to a random part and waiting till they’re finished.

Something like this should work for what you need, if you want it to wait a few seconds after movement, just add a wait(n) after MoveToFinished where n is the time to wait

local Children = workspace:WaitForChild("EasterEggs"):WaitForChild("PersonajeSecreto"):WaitForChild("MoveParts"):GetChildren()

local humanoid = script.Parent:WaitForChild("Humanoid")

while true do
	local randPart = Children[math.random(#Children)]
	humanoid:MoveTo(randPart)
	humanoid.MoveToFinished:Wait()
end

it doesn’t works but thanks for helping me

Are you getting any errors when attempting to run it??

i hve tried @Chrofex metod it works but it needs to be all the time moving to the parts but thanks to the feedback, can you contact me on discord and we talk more easily?
im TheRoblox#3275
thanks

Wait I realised that it takes in a position, I was giving it the part, my bad, try this

local Children = workspace:WaitForChild("EasterEggs"):WaitForChild("PersonajeSecreto"):WaitForChild("MoveParts"):GetChildren()

local humanoid = script.Parent:WaitForChild("Humanoid")

while true do
	local randPart = Children[math.random(#Children)]
	humanoid:MoveTo(randPart.Position)
	humanoid.MoveToFinished:Wait()
end

thanks all the persons for the feedback

What you should do is put this script inside a loop, only leaving the variable outside:

local Children = game.Workspace:WaitForChild("EasterEggs"):WaitForChild("PersonajeSecreto"):WaitForChild("MoveParts")

while wait() do
    for i, v in pairs(Children) do
	    if i == math.random(1, #Children) then
	    	script.Parent:WaitForChild("Humanoid"):MoveTo(v.Position)
	    end
    end
end

And finally detect when the movement of the NPC ends to be executed again using the MoveToFinished: Wait, this is how the script would look:

local Children = game.Workspace:WaitForChild("EasterEggs"):WaitForChild("PersonajeSecreto"):WaitForChild("MoveParts"):GetChildren()

while wait() do
    for i, v in pairs(Children) do
        if i == math.random(1, #Children) then
            script.Parent:WaitForChild("Humanoid"):MoveTo(v.Position)
            script.Parent:WaitForChild("Humanoid").MoveToFinished:Wait()
        end
    end
end```

complete thanks @Kriko_YT
you help me alot in discord