Best way to move many npcs to the player

I want my game to have a lot of npcs that walk towards the player to attack him
but to take into account that the player is moving and changing positions fast
i wanted to use WalkTo but in my game there are a lot of npcs and relaying on the humanoid will strain the server so whats a good alternative/optional way to make it work

1 Like

If you don’t want to use the pathfinding (WalkTo) then you could alternatively use TweenService: TweenService

1 Like

i understand but how do i use tweenservice to simulate walking like tweenservice work like how much time do u want an object to move form point A to point B and i dont want my npc to move light speed if the player is far away and how to i use it to make the npc to jump?

I don’t see how it makes much of a difference on the load on the server.
If you use TweenService, all you are essentially doing is setting the CFrame for every single frame.
WalkTo does make the NPC walk normally as if it was a player, the TweenService does not.

use for loops thingy

for i, Dummies in pairs(game.Workspace.Folder:GetChildren()) do
	if Dummies:FindFirstChild("Humanoid") then
		Dummy.Humanoid:MoveTo(game.Players:GetPlayers()[math.random(1, #game.Players:GetPlayers())].Character.HumanoidRootPart.Position)
	end
end
1 Like

One way would be to give SetNetworkOwner to the client and the client would use the MoveTo function

another option is for the server to only tell the clients the target and all clients would move the npcs independently

this video does not show how to make the npcs follow a target but it does show how the server would only tell the clients the target and all clients would inderpendtly make a local npc move to the target

also humanoids are expensive as they do lots of calculations so its hard to have many of them here are some demo projects that might help you increase your npc amount

500_enemys_running_at_60_fps.rbxl (36.2 KB)
SpawnEnemys.rbxl (36.7 KB)
Server_keeping_track_of_enemy_positions.rbxl (33.9 KB)
spawn_enemys_without_a_loop.rbxl (35.0 KB)
FollowPath.rbxl (42.9 KB)

8 Likes

ok but i have a question if i made them locally how do i keep tack on them in the server cuz i make them follow the player and not a constant part there is not a an optimize way to keep track on them that i can think of

can i ask why you need to keep track of there position?

there are hostile npc so they attack players therefore i need to keep track of them in order to my sainty checks and make them atttack the players
and when i use the npc in the video a player with high ping will probably glich their position and make them stuck somewhere while for other players they are moving (like the npc are not synchronize fully between players )

Instead of using separate scripts for each NPC, you should instead use CollectionService and loop through the NPCs with a designated tag, then do the code inside the loop.

This approach is more performant than separate scripts.

1 Like

yea i did that at first now i jusd put all my hostile npc in a folder and loop tohrugh the folder in the workspace but the question is alternative way to moveTo cuz the humanoid is straining the server

There is no “alternative” way to MoveTo().

Sure, there is Move() but that works entirely differently compared to MoveTo(). Could you show us your AI script?

so i will tell you this humanoid is very straining for the server its does many unnecessary calculates that strain the server therefore ppl started to make npcs without humanoids and use only the animation controller but the question is how does the npc moves form what i can see they move using tweenservice but how are they using tween to make them smooth or how does they simulate a jump with tween or how does they make the tween looks like normal walking speed that i dont know and im trying to search for a solution

I feel like you should try and check whether using Humanoid:WalkToPoint() would actually cause stress on the server or not because although people say that, it can depend on many factors.