Entity refusing to walk to random waypoints

Heya Everyone!!

I’m making an entity system and I’m trying to make it so that it’ll wander around random waypoints. The issue is that the entity refuses to walk towards them. Is there a reason as to why?

(This is the snippet of the module script. Aka, the entity system,)

--//Creating the entity
function BaseEntity:CreateEntity(EntityName)
	--//Finding entity
	local RequestedEntity = RegularFolder:FindFirstChild(EntityName) if not EntityName then
		RequestedEntity = SpecialFolder:FindFirstChild(EntityName) if not EntityName then
			warn("CANNOT FIND ENTITY: "..EntityName)
			return
		end
	end
	
	--//Creating entity
	local self = setmetatable({}, BaseEntity)
	
	self.Entity = RequestedEntity:Clone()
	self.Humanoid = RequestedEntity:FindFirstChildWhichIsA("Humanoid")
	self.Entity.Parent = game.Workspace
	
	--//List of entity behaviors & assinging them to their respective entity.
	self.EntityBehaviors = {
		["Starvlud"] = {
			["Behavior"] = function()
				self:Wander()
			end,
		}
	}
	self.EntityBehaviors.Starvlud.Behavior()

	return self
end

--//Default functions
function BaseEntity:Wander()
	local Walkpoints = workspace:WaitForChild("Walkpoints"):GetChildren()
	local RandomWalkPoint = Walkpoints[math.random(1,#Walkpoints)]
	
	while task.wait() do
		
		local NewPath = PathfindingService:CreatePath()
		NewPath:ComputeAsync(self.Entity.PrimaryPart.Position, RandomWalkPoint.Position)
		
		if NewPath.Status == Enum.PathStatus.Success then
			local Waypoints = NewPath:GetWaypoints()
			
			for _, Waypoint in pairs(Waypoints) do
				self.Humanoid:MoveTo(Waypoint.Position)
				self.Humanoid.MoveToFinished:Wait(2)
			end
		end
		
	end
end

Do you have any errors/warnings in the output?

Just a little concern that pathfinding every frame can be expensive especially for multiple entities, and you can also reuse the same Path object instead of creating a new one for each new path.

There isn’t any warnings nor errors in the output and I’ve decided to put the path creation code outside of the loop since you said it’s expensive to constantly be making new paths.

Hi. Try setting the primarypart of the entity thru the script, also self.Humanoid is referencing the template, not the clone youve created. I’m not sure if this was intentional or not, but if not change self.Humanoid to reference the clone

1 Like

Sorry for the late reply but yeah, setting the self.Humanoid to the clone managed to work. Thank you.

1 Like

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