Humanoid Model with smooth movement?

So, basically i’ve making a game what i used a system with body movers like “Pets Follow”, While i was testing the script i can see a delay when the server send informations to the Body Position.

I did a loop with Heartbeat that update the Current position of the Player on the Server,

game:GetService("RunService").Heartbeat:connect(function()

bp.Position = part.Position

bp.MaxForce = Vector3.new(math.huge,math.huge,math.huge)

bp.D = 700

bp.P = 15000

gyro.MaxTorque = Vector3.new(math.huge,math.huge,math.huge)

gyro.D = 100

gyro.CFrame = part.CFrame

end)

I want know a way to update the position of the Npc but at the same time Smooth.

https://gyazo.com/38d1bba3de1f37229ac6aa57de6ca40e

3 Likes

First, you should improve your existing code by:

-not updating the max force every heartbeat
-assigning a variable to math.huge (just for cleanleaness)
-indenting it
-grouping code together


local infinitevector = vector3.new(math.huge,math.huge,math.huge)
bp.MaxForce = infinitevector 
gyro.MaxTorque = infinitevector 

game:GetService("RunService").Heartbeat:connect(function()
	
	bp.Position = part.Position
	bp.D = 700
	bp.P = 15000
	
	gyro.D = 100
	gyro.CFrame = part.CFrame
	
end)

Second (probably useless), If it’s a humanoid, you’re better off using MoveTo() instead of body gyros.

someHumanoid:MoveTo(vector3.new(0,0,0)) --makes the humanoid move to the world's 0,0,0 position. can be used on players too!

Third, if you want less lag, the client needs to do it. You need to setup a remoteEvent in replicatedstorage for communication, and some kind of script to tell the server where to lead the humanoid. You also need to make sure the desired position isn’t too far away from the player on the server, and to make sure the traffic resulting from this isn’t too extreme.

local lastOffset --for checking position, mentioned below
while wait(0.5) do --0.5 seconds is good enough, i guess? 
	local currentOffset  --your offset, whatever you're using
	if currentOffset ~= lastOffset then --we're making sure we're not passing pointless arguments
		someRemoteEvent:Fire(currentOffset) --we fire to a remote event
               lastOffset = currentOffset --sorry for the indenting, this is edited in
 	end
end	

Now, we need to adapt the server to this.

	theSameEventFromBefore.OnServerEvent:Connect(function(player,target) --may have errors
		local playerPosition = player.Character.HumanoidRootPart.Position --for checking if the player wants to move the NPC too far away from themselves, eg. when they're exploiting
		local magnitude = (playerPosition - target).Magnitude --distance between desired position and player position
		if not magnitude > 10 then --whatever you would see fit for something like this is okay (10 is just an example)
			--update the part position or something
		end
	end)

Please don’t paste this literal code into your script. it needs changes for your specific use case, as i have no clue what you are using for position.

2 Likes