Hats System

Hello im making a hat system for my gamemode and I’m stuck the hat in this case Santa hat is allays on the left of the head and i cannot get it to go to the top of the head

Screenshot:

http://cloud-4.steamusercontent.com/ugc/45375528347392907/7E9EDFCE923CD97F319FBF08B99D155E68237F2A/1024x576.resizedimage

[LUA]
local hats = { }

local function CreateHat( pl, model )
SafeRemoveEntity( pl.Hat )

local hat

hat = ClientsideModel( model, RENDERGROUP_OPAQUE )
hat:SetNoDraw( true )

pl.Hat = hat
hat.Owner = pl
table.insert( hats, hat )

end

local function CleanHats( )
local k, v

for k, v in pairs( hats ) do
	if not ValidEntity( v ) then
		hats[ k ] = nil
	elseif not ValidEntity( v.Owner ) then
		SafeRemoveEntity( v )
		hats[ k ] = nil
	end
end

end

local function DrawHats( )
local k, v, pos, ang

CleanHats( )

for k, v in ipairs( player.GetAll( ) ) do
	if v == nil then continue end
	if v == LocalPlayer() then continue end
	if not v:Alive( ) and ValidEntity( v.Hat ) then
		SafeRemoveEntity( v.Hat )
		v = v:GetRagdollEntity( )
	end

	if not ValidEntity( v.Hat ) then
		CreateHat( v, table.Random(EMSRP.hats).model )
	end

	pos, ang = v:GetBonePosition( 6 )

	v.Hat:SetPos( pos )
	v.Hat:SetAngles( ang )
	v.Hat:FollowBone( v, 6 )
	v.Hat:DrawModel( )
end

end
hook.Add( “PostDrawOpaqueRenderables”, “Draw Hats.PostDrawOpaqueRenderables”, DrawHats )
[/LUA]

I take no credit for this code i just edited this.

Angle:RotateAroundAxis

I tried doing
[LUA]
v.Hat:SetAngles( ang:RotateAroundAxis( ang:Up(), 90 ) )
[/LUA]
but it just creates an error sorry never used that func before and im not great at lua
Error:



Error in hook PostDrawOpaqueRenderables: gamemodes/emsrp/gamemode/modules/hats/cl_hats.lua:49: bad argument #1 to 'SetAngles' (Angle expected, got no value)
stack traceback:
	[C]: in function 'SetAngles'
	gamemodes/emsrp/gamemode/modules/hats/cl_hats.lua:49: in function 'fn'
	addons/ulib/lua/ulib/shared/hook.lua:179: in function <addons/ulib/lua/ulib/shared/hook.lua:162>
	[C]: in function '•‬••‎'

[ERROR] 
  1. unknown - [C]:-1


ang:RotateAroundAxis( ang:Up(), 90 )
v.Hat:SetAngles( ang )

Thanks that worked

[editline]7th April 2015[/editline]

Randomly i get this error



Error in hook PostDrawOpaqueRenderables: gamemodes/emsrp/gamemode/modules/hats/cl_hats.lua:42: attempt to index local 'v' (a nil value)
stack traceback:
	gamemodes/emsrp/gamemode/modules/hats/cl_hats.lua:42: in function 'fn'
	addons/ulib/lua/ulib/shared/hook.lua:179: in function <addons/ulib/lua/ulib/shared/hook.lua:162>
	[C]: in function '•‬••‎'

[ERROR] 
  1. unknown - [C]:-1


and also the hats occasionally spaz out and glitch in front/behind the player models (ill get a screenshot hopefully)