Help with a hat

This is a basic script to make a spider float above your head. But I see a problem with it that I don’t know how to fix, the problem is that it lag’s behind the player when he runs. I was wondering if anyone could help me with this. Any help will be very appreciated.

This is the init.lua file
[LUA] AddCSLuaFile(“shared.lua”)
AddCSLuaFile(“cl_init.lua”)

include(“cl_init.lua”)

function ENT:Initialize()

self:SetModel("models/props_halloween/smlprop_spider.mdl")

self:SetMoveType(MOVETYPE_NOCLIP)
self:SetSolid(SOLID_NONE)

end

function ENT:Think()
if not IsValid( self.ply ) then

	self:Remove()

	return
end

self:SetPos( self.ply:GetPos() + Vector( 0, 0, 85 ) )

end

local function Togglehat( ply )

if not IsValid( ply.hat ) then
	ply.hat = ents.Create("hat")
	ply.hat.ply = ply
	ply.hat:SetPos( ply:GetPos() + Vector( 0, 0, 85 ) )
	ply.hat:Spawn()
	ply.hat:Activate()

else

	ply.hat:Remove()

end

end
concommand.Add(“Test1”, Togglehat )

local function Removehat( ply )

if IsValid( ply.hat ) then

	ply.hat:Remove()

end

end
hook.Add(“PlayerDisconnected”, “Disc_RemoveIndicator”, Removehat )
hook.Add(“PlayerDeath”, “Die_RemoveIndicator”, Removehat ) [/LUA]

this is the shared.lua
[LUA]ENT.Type = “anim”
ENT.Base = “base_gmodentity”
ENT.PrintName = “Hat 1”
ENT.Author = “Loki611”
ENT.Spawnable = false
ENT.AdminSpawnable = false[/LUA]

this is the cl_init.lua
[LUA] include(“shared.lua”)

function ENT:Draw()
self:SetModelScale(0.6, 0)
self:DrawModel()
end[/LUA]

Add self:NextThink(CurTime() ); return true at the end of your Think function, but you should just handle the positioning clientside instead of serverside.

Thank you so much, turns out it really does’t work how I wanted it to though. No to sound like i’m begging but could you maybe show me how I could set it to follow the head? Because right now if I duck it doesn’t"t follow.

On the client start a 3d cam inside a RenderScreenspaceEffects hook, inside the cam draw a clientside model at the position of the player who’s wearing the hat’s “eyes” attachment.

I only somewhat understand what you’re saying. I’m kinda new to all this, Could you show me a example?

[lua]
hook.Add(“RenderScreenspaceEffects”, “RSSE”, function()

cam.Start3D()

for owner, c_model in pairs( someHatTable ) do

if (owner == LocalPlayer()) then continue; end // Or do some thirdperson calculations

local id = owner:LookupAttachment(“eyes”);
c_model:SetPos( owner:GetAttachment(id).Pos );
c_model:SetAngles( owner:GetAttachment(id).Ang );
c_model:Draw();

end

cam.End3D()

end);
[/lua]

Why not just use an entity?

What do you mean? what I’m trying to do is make a entity follow a player’s head, so when he duck or jump’s the entity follows. Also Chessnut thank you so much for telling me to put the position in client side. Also >>oubliette<< I can’t get my Garry’s Mod to open right now so I can’t say if it’s working or not.

My code was fully clientside and more efficient then networking entity data and having them drawn clientside should stop the hat lagging behind the player as they move.

You can easily look at the pointshops code and implement it since it uses an efficient way of placing hats with ClientsideModels.
eg. bones, attachment positions, etc.

Well i’m starting to mess thing’s up. I’ll come back to this later when I learn more about LUA

You can find it in the pointshop codes.