How do I fix this model lagging behind?

local RS = game:GetService("ReplicatedStorage")
local Event = RS.HotPotatoSelected
local Start = RS.GUIEvents.SecondEvent
Event.OnServerEvent:Connect(function()
	local Players = game.Players:GetPlayers()
	local random = Players[math.random(1,#Players)]
	Start.OnServerEvent:Connect(function()
		
		
			game.Workspace.Potato.Position = random.Character.HumanoidRootPart.Position
		workspace.Potato.Parent = random.Character.HumanoidRootPart
		while true do
			random.Character.HumanoidRootPart.Potato.Position = random.Character.HumanoidRootPart.Position + Vector3.new(0,0,0)--The Vector3 appears to do nothing but without it, the potato is offset
			wait()
		end
		
		
	end)
end)

The potato’s position is constantly updated to be at the position of the selected player’s HumanoidRootPart. But the model is always lagging behind and doesn’t move perfectly with the player. Is there a better method of doing what this code does so the model doesn’t lag behind?

It seems that every time Event is fired, more connections is made to Start. The following code block is for analysis only. Other than that, I’m telling you that the lag is always there. Why are you making the potato follow a player’s character?

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Event = ReplicatedStorage.HotPotatoSelected
local Start = ReplicatedStorage.GUIEvents.SecondEvent
Event.OnServerEvent:Connect(function()
	-- Anyone could fire this and continue operations...
	local players = Players:GetPlayers()
	local random = players[math.random(#players)]
	Start.OnServerEvent:Connect(function()
		-- Refer to the previous comment
		-- This event will always target the same player it first randomly chose lol
		-- and there is only one potato at a time
		workspace.Potato.Position = random.Character.HumanoidRootPart.Position
		workspace.Potato.Parent = random.Character.HumanoidRootPart
		while true do
			-- This will never stop
			random.Character.HumanoidRootPart.Potato.Position = random.Character.HumanoidRootPart.Position + Vector3.new(0,0,0)--The Vector3 appears to do nothing but without it, the potato is offset
			task.wait()
		end
	end)
end)

I’m making the potato follow the character because I want it to appear like they’re a giant potato, but I want to keep the model of the player intact because I will later make it so that the potato model is transferred from player to player. Should the potato be more of something like a hat that is put on the player?

Also, I have plans to make that while loop break once it is passed on

Then why don’t you try using the Accessory method way-ish? Here is how it’s done:

  1. Create an Accessory instance, name it Potato.
  2. Change the potato part name Handle, parent it to the Accessory instance
  3. Create an Attachment and name it as BodyFrontAttachment(very important to check typos if any!)
    Screenshot 2022-11-25 at 23.43.44
  4. Parent the Attachment to the Handle
  5. Have the Potato(accessory) in any storage container accessible from a script
  6. For each event, parent a clone of the accessory to player.Character
  7. Parenting it on the character will not result in “lagging” behavior! Hurray!

I’ll try this out to see if it works out. Thanks!

The potato is a mesh. Can a mesh be a handle?

Definitely as long it is any form and derivation of BasePart class. Just try it and see…

I’m gonna sound stupid. What’s the BasePart class?

Consult the documentations first before you ask. It’s generally anything that acts and looks like a part. Unions are example that can be unions. Check with this:

Union:IsA("BasePart") -- true

https://create.roblox.com/docs/reference/engine/classes/BasePart

Alright that makes sense. Thank you, and sorry I didn’t check the documentation before hand

If you’re wondering why it was lagging behind, it’s because the client controls movement, but the server controlled the position of the potato.

Client says “Hi, I’m over here”
(100ms)
Server says “OK, moving the potato”

Everything worked as intended, except for one thing. The potato is rotated the wrong way. When I tried setting the handle’s orientation, I got an error message saying: Orientation is not a valid member of MeshPart “Potato.Handle”.

PotatoClone.Handle.Orientaion = Vector3.new(0,0,-90)

Oh yeah, I forgot about something. Keep playing around with the placement of the attachment. Its orientation and relative position to the handle matters!

Or just put one on a dummy, run the game, adjust the values on the accessory on the dummy and then copy those properties.

So I would change the orientation of the accessory instead of the handle?

No! I said the attachment within the handle. It’s the thing that latches onto your character!

Okay. I’ll mess around with the properties of that attachment and see if I can get it to a good state

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