So I have made a pet system but whenever I walk around the pet occasionally lags. It doesn’t happen all the time but from time to time it just stops moving for a second and just teleports to the player. Here is a video
So I am doing the pet movement server side as I want it to update for all players. Below is the code for the pet system. I have not included most of it as most of it is just calculating the position it needs to move to and that works fine. I have just included the most important parts. If you need the whole script then please tell me and I will upload it.
game.Players.PlayerAdded:Connect(function(player)
while wait() do
...
bodyPos.Position = humRootPart.Position + humRootPart.CFrame.LookVector * Z + humRootPart.CFrame.RightVector * X + Vector3.new(0, 2, 0)
bodyGyro.CFrame = humRootPart.CFrame
...
end
end)
I have already tried to move this to another script because I thought that maybe some other code in the script was causing lag but it still does it. I’m not really sure how to fix this. Any help would be hugely appreciated.
It could quite possibly be server delay either from the server itself or your ping. Your best bet is to do this locally. It will appear smooth for the player who owns the pet but might sometimes stutter (like above for players with bad ping) as other players may not accurately have the players position (since there will sometimes be delay between client and server).
Also instead of constantly setting the position in a while loop, why not update it whenever the player moves (for optimization purposes)? Assuming that’s what you want.
To add onto what @Ze_tsu said, it could also because wait() can be slow at times and can wait longer than it would normally. You could try to connect the movement changing to the Heartbeat event or, to be more optimal like he said, do it whenever the player’s character moves, which you can probably do by the Humanoid.Running event instead of a while wait() do loop
This shouldn’t be done server-side! Or atleast not completely…
I do the following for my pet system:
A server script handles the pets for all players currently in-game. When a player changes a pet this script notices it (RemoteEvent) and fires an updated table to all players.
On join, a player requests the pet table from the server script and calculates pet positions for all players in-game.
Every player calculates the pets position on their client. This removes strain on the server. Additionally you could have a “maximum range”, beyond that range that player’s pet won’t be moved (or at a lower interval).
Thanks for your contribution. I will definitely try to optimise it like you said. However for the solution I found that setting the network ownership to the player actually fixes this. In this article it says that that the switching automatically done by roblox can cause some delay. I believe this is what was causing the issue…
The problem with setting NetworkOwnerShip is that it will only ever be smooth for one player at a time. It might be smooth for you but will be laggy for other players.
I understand however before the fix it looked smooth for everyone. The pets were following other players smoothly just with the occasional lag mentioned in the post. This fix shouldn’t change that (It should just fix the occasional lagging) however I will do some testing.
Yes it will fix the lag for one player.
Setting the NetworkOwner of a part to a player basically means: use this player’s device to calculate the physics for this part. As that player calculates the physics there is no lag for them. Those calculations have to be replicated over to other players, through the internet (which can be slow sometimes). This will cause lag for other players.
See my previous reply on how I handle pets in my game.
I understand there will be some lag but as you said it will do the calculations on the client and then replicate it to the server and then replicate it to everyone else. There is no other way around the fact that anything calculated on the client has to go through that process. In your solution the client still calculates the position and has to send it’s calculations over the internet to the server and then the server has to send it to every one else. This will always cause lag.
It’s actually better and quicker to do the calculations on the server as the server just updates the positions globally instead of having to wait for the client to send their calculations. The calculations also really aren’t that complex so it doesn’t put much strain on the server.
Calculating on the client also isn’t the best idea because if that player has a high ping then it becomes laggy for everyone instead of just that one player.
That is not how I handle pet movement.
I do all calculations on each client. The server is only involved in making sure that the right pet is “given” to the right player. All movement calculations are done on each client separately.
I hope this clears things up.