Accessories

Is there a way to make it so when a player spawns he gets a hat on his head? I am making an accessories npc but really just need the actual accessories code, can anyone please provide me with it or help me?

We can help.

What was the point of that?

Anyway, to answer the question, it’s simple. Clientside, when a player spawns, create a clientside model (

Global.ClientsideModel), and save it as a vaiable on a player. Now, you can either parent it to the player using

Entity:SetParent and

Entity:LookupAttachment, or you could set the position and angle manually in a think hook (

GM/Think). When the player disconnects, delete the Clientside model (as it doesn’t get collected with the garbage). Don’t forget to draw the model in

GM/PostDrawOpaqueRenderables

This is the code I use: (ignore the ITEM stuff, I copied it directly from my inventory system)

[lua]
function ITEM:CreateHat( )
self.Hat = ClientsideModel( self.Model , RENDERGROUP_OPAQUE )
self.Hat:SetNoDraw( true )

local index = 1
if ( self.Offset[self.Player:GetModel()] ) then index = self.Player:GetModel() end
self.Hat:SetModelScale( self.Offset[index].Scale, 0 )

end

function ITEM:OnEquip( _Player )
self.Equiped = true
self.Player = _Player

if not self.Hat and CLIENT then
	self:CreateHat()
end

end

function ITEM:OnHolister( _Player )
self.Equiped = false
self.Player = false

if self.Hat and CLIENT then
	SafeRemoveEntity( self.Hat )
end

end

function ITEM:CalculateOffset( pos, ang )

local index = 1
if ( self.Offset[self.Player:GetModel()] ) then index = self.Player:GetModel() end
local Voffset = Vector( self.Offset[index].Vector.x, self.Offset[index].Vector.y, self.Offset[index].Vector.z) * ( self.Player:GetModelScale() or 1 );

Voffset:Rotate( ang )

ang:RotateAroundAxis(ang:Right(), 	self.Offset[index].Angle.r); --Rotate around the axis (to the right) -90 degree's
ang:RotateAroundAxis(ang:Up(), 		self.Offset[index].Angle.p); --Rotate around the axis (upwards) 90
ang:RotateAroundAxis(ang:Forward(), self.Offset[index].Angle.y);
pos = pos + Voffset

return pos, ang

end

function ITEM:PostDrawOpaqueRenderables( )
if not self.Player or not self.Equiped then return end
if self.Player == LocalPlayer() and not self.Player:ShouldDrawLocalPlayer() then return end
local Ent = self.Player
if not self.Player:Alive() then Ent = self.Player:GetRagdollEntity() end

if (IsValid(Ent) and hook.Call( "InventoryShouldDrawHats", GAMEMODE, self.Player )) then
	local pos, ang = self:CalculateOffset( Ent:GetBonePosition( self.BoneIndex ) )

	local index = 1
	if ( self.Offset[self.Player:GetModel()] ) then index = self.Player:GetModel() end
	self.Hat:SetPos( pos )
	self.Hat:SetAngles( ang )
	self.Hat:SetModelScale( self.Offset[index].Scale, 0 )
	self.Hat:DrawModel( )
end

end
[/lua]

Don’t copy paste the code directly because there is an error in the CalculateOffset function (the angles are glitchy and I still need to fix it, haven’t gotten round to it yet cause it isn’t a major problem unless you want to set the pitch component of the hat)

Thanks, alot but your code seems very advanced as i am a very new lua coder, and know basically nothing. I’ll try something.

[editline]29th April 2015[/editline]

Also would your code “Inventory” System somehow be PERP?

[editline]29th April 2015[/editline]

Also i see theres a addon called PAC, if i do the positioning in there, is there a way to get the code to place it on playermodel?

Sorry if it is advanced. Code starts off simple and the more bugs you find, the more checks get put in, and after a while, 10 lines turn into 100. If you follow the guide I put above the code, you should be able to do it no problem. The code was just an example. No, my inventory system isn’t PERP, it’s for my own private gamemode (won’t be released or even hosted, it’s just a place where I can make various functions interact to make cool shit and then release as a separate addon). As for PAC, I don’t know, as I’m not the developer of that addon. You would do best to ask him.

Okay thank you!

Do you ever try and do something for yourself? Sorry for potentially coming across as rude, but almost every single thread you have made you have practically begged for people to do it for you. And in one thread a mod has noted on the detrimental side of simply supplying someone with code, or rather, spoon feeding them.

In future, just search for threads where people have asked similar questions, save yourself the time and effort of asking when your question has already been answered somewhere already. Use the wiki. For christ sake everything you need to know is on there, and all you need to do is put in a little effort into making it work for yourself.

It’s been noted on multiple occasions that you are attempting to make your own PERP server, and more than likely without the proper legal agreement to do so. For what you are trying to accomplish you are probably going about it the absolutely worst way possible.