parent prop to player

I am trying to create a script that parents an entity that I made to a players head, but how do I exactly do that?
I got slightly confused by the wiki so I hope someone can clear this up for me

I got this so far:


local function init()
	local test = ents.Create("ent_test")
	if ( !IsValid( test ) ) then return end 
	test:SetMoveType( MOVETYPE_NONE )
     for k, v in pairs(player.GetAll()) do
        test:SetParent() // what now :/
     end
end
hook.Add( "Initialize", "xoxo", init )

Basically what i am trying to achieve here is, that whenever a player joins he gets a prop attached to him

GM:PlayerSpawn or

GM:PlayerInitialSpawn, parent the entity to the player argument

Entity:SetParent - Parent entity and, optionally, attachment id, must be specified.

I never really worked with entities, but from what I can see you should do [lua]local plr = Player(1) – Get a player from somewhere

local myEnt = ents.Create(“some entity”)
myEnt:SetParent(plr)[/lua]or something like this.

And what exactly are you trying to achieve by iterating through all the players?

Yeah I got that far already, I think I am only stuck at the parenting part

I just figured as much, looping here is pretty useless.
I still dont grasp the way of parenting the ent to the player though

You just do this in one of the hooks I gave you, assuming the argument is called “ply”
[lua]
test:SetParent(ply)
[/lua]

If you don’t know how hooks work, I recommend you read this: http://wiki.garrysmod.com/page/Hook_Library_Usage


hook.Add( "PlayerSpawn", "xoxo", function(ply)
	local test = ents.Create("ent_test")

	if ( !IsValid( test ) ) then return end 

	     test:SetMoveType( MOVETYPE_NONE )
	     test:SetParent(ply, 6)
	 

end)

But the script doesnt know where to attach it to now does it? And I feel like I am missing something here

EDIT: So I add a boneID I get it, but after adding like number 6 after ply it still doesnt appear? What am I doing wrong?

You need to call :Spawn() after creating an entity. It’s shown in the example of this, not sure how you missed it:

ents.Create

And I have that in my code, its not appearing

Show the code of your ent_test

You don’t use bone ID, you use attachment ID, here’s a list:



1:
id	=	1
name	=	eyes
2:
id	=	2
name	=	lefteye
3:
id	=	3
name	=	righteye
4:
id	=	4
name	=	nose
5:
id	=	5
name	=	mouth
6:
id	=	6
name	=	tie
7:
id	=	7
name	=	pen
8:
id	=	8
name	=	chest
9:
id	=	9
name	=	hips
10:
id	=	10
name	=	lefthand
11:
id	=	11
name	=	righthand
12:
id	=	12
name	=	forward
13:
id	=	13
name	=	anim_attachment_RH
14:
id	=	14
name	=	anim_attachment_LH
15:
id	=	15
name	=	anim_attachment_head


But if I were you I’d use something like



test:LookupAttachment("mouth")


just as an example.

Also you should first set the position of the entity to wherever you want it to be placed, try something like this:



mouthAtt = test:LookupAttachment("mouth")
tab = test:GetAttachment(mouthAtt)

pos = tab.Pos
ang = tab.Ang

test:SetPos(pos)
test:SetAngles(ang)


AAAAND make sure it has no collisions or it will block the player.

Shouldnt I be looking for the players attachment?

Its just a basic entity with no properties, it spawns just a prop

If you are expecting something like a hat or backpack being attached to a player, then this is not for you
This will attach a prop into the player, but it will cause issues like collisions and that problems

Can you suggest a better way to do this? I dont really want to buy or use pointshop addons

Try drawing the model clientside on the client.

GM:PostPlayerDraw

Draw the model there, also you can read how pointshop actually draw models into players, but there’s no a prop parenting functionallity, it’s just running drawmodel and applying position inside that hook