Fixing a script about pathfinding from YouTube. (help)

I used a script from this video for pathfinding on the client side:

Now the normal script causes multiple NPC’s to move one by one and the next will move or spawn after the time I set in the wait().


local GetPosition = function(folder)
	local data = characters[folder]
	data = data[math.random(#data)]
	local offset = Vector3.new(math.random(-data.size.X / 2, data.size.X / 2), 0, math.random(-data.size.Z / 2, data.size.Z / 2))
	return data.cFrame:PointToWorldSpace(offset)	
end

for i, folder in ipairs(workspace.Enemies:GetChildren()) do
	for _, enemyFolder in ipairs(folder:GetChildren()) do
		local enemyNameTag = folder:GetAttribute("EnemyName")
		local data = {}
		for i, part in ipairs(enemyFolder:GetChildren()) do
			table.insert(data, {["cFrame"] = part.CFrame, ["size"] = part.Size})
		end
		characters[enemyFolder] = data
		enemyFolder:SetAttribute("Position", GetPosition(enemyFolder))
		enemyFolder:SetAttribute("RoamTimer", math.random(3,6))
	end
end

while true do
	for folder,data in pairs(characters) do
		folder:SetAttribute("Position", GetPosition(folder))
		wait(2)
	end
end

https://gyazo.com/4fe0da1515dcada2b1e290d500ed9bd6

If I move the wait(2) below the pairs loop it causes a weird bug where for SOME reason they go on the same path? Can somebody explain this please.

while true do
	for folder,data in pairs(characters) do
		folder:SetAttribute("Position", GetPosition(folder))
	end
	wait(2)
end

I have printed and debugged for a while and theres nothing that resembles them to have the same destination.

https://gyazo.com/b4100fbb2bd3bf7850219de8ffe2e696

(idk why the gyazo’s don’t embed)

Sadly I cannot help you fix this script mainly because my brain is in shambles, but I can help recommend you start all over with a simpler method using a Modular Script.

SimlpePath

1 Like

that’s a strange bug I cant see why it would be doing that

in the video is a link to my discord if you message me there ill be happy to look into your problem deeper and try to work out what is going on

i’ll look into that thanks :grinning: (30 words issue)