How do i change the player model

Hi facepunch.

Can someone tell me how to change the players model with lua?

Thanks in advance

You need to get a player’s player object and apply SetModel() to it.

The best way to do it is inside a gamemode hook such as PlayerSpawn.

[lua]hook.Add(“PlayerSpawn”,“PlayerModelHook”, function(ply) – Here ply is the player’s object of the player who has just spawned.
ply:SetModel(“path/to/model.mdl”) – Change that to a valid model
end)
[/lua]
:eng101:

http://wiki.garrysmod.com/?title=Entity.SetModel <-- We use Entity.SetModel because a player is an entity
http://wiki.garrysmod.com/?title=Hook.Add

[editline]01:11PM[/editline]

Also a direct way to grab a player’s object without hooks is player.GetByID().

So you could do this in console to change your model :


player.GetByID(1):SetModel("path/to/model.mdl") -- Change that to a valid model  


for k,v in pairs(models) do
	local icon = vgui.Create( "SpawnIcon", IconList ) 
	icon:SetModel( v )
	icon.DoClick = function( icon ) surface.PlaySound( "vo/npc/male01/thislldonicely01.wav" ) ply:SetModel( v )  end 
 	IconList:AddItem( icon )
	end

This does not work

[editline]05:37PM[/editline]

And could you also tell me how to add plain text to a derma (Not a text box)

This doesn’t work because it is clientside and ply is not a defined value.
Clientside the client’s object is obtained with LocalPlayer() but that won’t help you since you need to do all model changes serverside. So you would have the player run a console command with the model as an argument.

This should work, but is untested :

[release]Clientside[/release]
[lua]local v = “path/to/model.mdl”
RunConsoleCommand(“modelchange”, v)[/lua]

[release]Serverside[/release]
[lua]function ChangePlayerModel( ply, command, arguments )
local model = arguments[1]
ply:SetModel(model)
end

concommand.Add( “modelchange”, ChangePlayerModel)[/lua]

For text use this :

and you should probably take a look at the library :

Thank you