Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

My npc's pathfinding is super laggy and the npc stops every once and a while, does anyone know why?

Asked by 4 years ago

I went along with a tutorial for a pathfinding npc on youtube and changed it around so it fits my npc, and the pathfinding is super laggy and I don't know why.

001local myHuman = script.Parent:WaitForChild("Humanoid")
002local myRoot = script.Parent:WaitForChild("HumanoidRootPart")
003local head = script.Parent:WaitForChild("Head")
004local lowerTorso = script.Parent:WaitForChild("LowerTorso")
005local grab = script.Parent:WaitForChild("grabanim")
006local grabAnim = script.Parent.Humanoid:LoadAnimation(grab)
007grabAnim.Priority = Enum.AnimationPriority.Action
008 
009 
010 
011local clone = script.Parent:Clone()
012 
013function walkRandomly()
014    local xRand = math.random(-50,50)
015    local zRand = math.random(-50,50)
View all 173 lines...

1 answer

Log in to vote
0
Answered by 4 years ago

Network Ownership

In a Roblox game, all non-anchored parts are “physically simulated” — they can fall, bounce, float in water, etc. All of these things are handled automatically by Roblox so you don’t have to write your own physics engine.

When a Roblox game runs, several computers/devices are involved. To divide the work of calculating physics, Roblox automatically assigns parts to either the server or to a client. In general, if a part is near an in-game character, its physics will be calculated by that player’s device; otherwise it will be calculated by the server. In either case, the server or client that calculates a part’s physics is called its owner.

Since physics updates must be sent over the network, there’s a small delay between when the owner makes the physical changes and when the other devices see those changes. Normally this delay isn’t too noticeable, but issues may occur when part ownership changes.

Example: Projectiles

Imagine a player is shooting an object at another player.

Problem

As the object travels through the air, it will get far from the shooter and ownership will switch to the server. Once the object gets close enough to the target player, it will switch ownership to the player. Even with a good network connection, there may be a tiny delay and visible “hop” when the object’s owner switches.

Solution

Manually set ownership of the projectile to the server using SetNetworkOwner(). This ensures that the owner does not change, preventing visible “hops.”

SetNetworkOwner() and its partner function GetNetworkOwner() must be set on the server side. Clients are not allowed to change ownership settings in a LocalScript.

Your script

In your script, the NPC networks owner is changing from client to server, which will make it stutter a few times, delaying it's time to get to the destination.

We can use :SetNetworkOwner, to prevent it from stuttering. Put this at the top of your script

1if script.Parent.HumanoidRootPart:CanSetNetworkOwnership() then
2    script.Parent.HumanoidRootPart:SetNetworkOwner(nil) -- setting the owner to the server
3end
0
i did some tests with this and from what i saw all the issues are fixed, thanks! CelticFury600 4 — 4y
0
No problem! CaIcuIati0n 246 — 4y
Ad

Answer this question