Need some help with a script

This script is supposed to animate a pet to randomly go to the position of a part, but for some reason the script is not working.

local seed = 3
local p = script.Parent.Humanoid -- Links to the Humanoid in the pet
function r()
	seed = seed + 3
	math.randomseed(tick() + seed)
end

function go(part)
	p:MoveTo(part.Position)
	p.MoveToFinished:Wait()
end
local locsv = game.Workspace:WaitForChild("WanderPoints")
local loc = locsv:WaitForChild("Start")
local locs = locsv:GetChildren()

while true  do
	local locs = locsv:GetChildren()
	go(locs)
	local tab = {}
	for i = 1, #locs, 1 do
		local sub = locs[i]
		if (loc.Position-sub.Position).Magnitude <= 32 and sub ~= loc then
			table.insert(tab, #tab + 1,sub)
		end
	end
	r()
	local newVar = math.random(1, #locs)
	loc = locs[newVar]
	go(loc)
	wait()
end

Here are some photos of my workspace
The Pet – The brain script is the one that controlles the pet
Screenshot 2022-04-24 151553

The Folder with all the points for the pet to move to – Start is were the pet starts

Screenshot 2022-04-24 151750

1 Like

Instead of using a while true do loop, you can use an ipairs loop, which loops through every item of a table, which :GetChildren returns a table.

Ok, how do i add this into my code and were?

1 Like

You can read about ipair and pair loops here. pairs and ipairs | Roblox Creator Documentation. they are written like this:

local table = {"Apple", "Banana", "Cherry"} -- this is an example table
for i, value in ipairs(table) do
print(value)
end

The expected output would look like:

Apple
Banana
Cherry

iPair loops go through each item of a table. The example given has I as which number of the table the ipair is on. (In the example about… Apple = 1, Banana = 2, etc.) and value is equal to the actual value. Which is how we print it. You can work with it that way.

for i = 1, #locs, 1 do -- < Try putting an ipair here. 
		local sub = locs[i]
		if (loc.Position-sub.Position).Magnitude <= 32 and sub ~= loc then
			table.insert(tab, #tab + 1,sub)
		end
	end

If this helped let me know!
Sorry had to edit this as I made a small mistake!