Better way of following player?

Hello, I’m trying to make an entity stay at the players position, but when the player moves it lags behind the player and not on him, at the moment, I’m using:


timer.Create( "position", 0, 0, function()
	test:SetPos( ply:GetPos() + ply:GetAngles():Up() * 35 + ply:GetAngles():Forward() * 6 )
	test:SetAngles( ply:GetAngles() + Angle(90, 0, 0 ) )
end)

I’ve tried followbone but the angle messes up.

Put it in a think hook instead of a timer?

Does the same.

Are you running this in both realms? Running it in server and not in client will cause that warping, but if you’re coding that entity, use this

Entity:StartMotionController

Been giving it ago, but it seems to spawn and just stay, and the server one just stays and lags.

Server:



function ENT:Use(ply)

	self:Remove()

	knife = ents.Create("prop_physics")
	knife:SetModel("models/props_junk/meathook001a.mdl")
	knife:SetPos( ply:GetPos() + ply:GetAngles():Up() * 35 + ply:GetAngles():Forward() * 10 )
	knife:SetAngles( ply:GetAngles() + Angle(90, 0, 0 ) )
	knife:Spawn()
	knife:SetMoveType( MOVETYPE_NONE )
	knife:SetSolid( SOLID_NONE )

	local i = knife:GetPhysicsObject()
	i:EnableMotion(false)
	i:Sleep()

	hook.Add( "Think", "test", function()
		knife:SetPos( ply:GetPos() + ply:GetAngles():Up() * 35 + ply:GetAngles():Forward() * 6 )
		knife:SetAngles( ply:GetAngles() + Angle(90, 0, 0 ) )
	end)

	net.Start( "test" )
		net.WriteEntity( knife )
	net.Send( ply )

end


Client:



hook.Add( "Think", "test2", function()
	net.Receive("test", function()

		local p = net.ReadEntity()

		p:SetPos( ply:GetPos() + ply:GetAngles():Up() * 35 + ply:GetAngles():Forward() * 6 )
		p:SetAngles( ply:GetAngles() + Angle(90, 0, 0 ) )
	end)
end)


Nono, that’s bad in every aspect, why it’s net.Receive inside a think hook, why are you creating a think hook inside use

Still wrong? Does the same.

Server:



function ENT:Use(ply)

	self:Remove()

	knife = ents.Create("prop_physics")
	knife:SetModel("models/Gibs/HGIBS_spine.mdl")
	knife:SetPos( ply:GetPos() + ply:GetAngles():Up() * 35 + ply:GetAngles():Forward() * 10 )
	knife:SetAngles( ply:GetAngles() + Angle(90, 0, 0 ) )
	knife:Spawn()
	knife:SetMoveType( MOVETYPE_NONE )
	knife:SetSolid( SOLID_NONE )

	local i = knife:GetPhysicsObject()
	i:EnableMotion(false)
	i:Sleep()

	net.Start( "location" )
		net.WriteEntity( knife )
	net.Send( ply )

end

function ENT:Think()
	net.Receive("location", function()

		local p = net.ReadEntity()

		if p:IsValid() then
			p:SetPos( ply:GetPos() + ply:GetAngles():Up() * 35 + ply:GetAngles():Forward() * 6 )
			p:SetAngles( ply:GetAngles() + Angle(90, 0, 0 ) )
		end
	end)
end


Client:



local function test()

	net.Receive("location", function()

		local e = net.ReadEntity()

		if e:IsValid() then
			e:SetPos( LocalPlayer():GetPos() + LocalPlayer():GetAngles():Up() * 35 + LocalPlayer():GetAngles():Forward() * 6 )
			e:SetAngles( LocalPlayer():GetAngles() + Angle(90, 0, 0 ) )
		end
	end)

end


Sorry, new to making entities.

No no no. What the heck?

1.) Why are you calling net.Receive in ENT:Think() and in a local function. Put the one on the client outside the function bare…

2.) Delete the net.Receive on the server. If you have the variables right there, just call the code in ENT:Use().

3.) net.Receive is like a hook. It’ll call when it gets sent. Read the net library usage.

4.) Also, of course it’s out of sync. You are having a net message that doesn’t even need to be networked sent to the client. Keep your code in the ENT:Use() as it can all be done serverside.

5.) In ENT:Use(), you probably want the person who actually pressed ‘e’ on the knife, so you should actually be using the second argument, caller.

6.) You should check to see if the caller is a player.