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:
[url]http://wiki.garrysmod.com/?title=Gamemode.PlayerSpawn[/url]
[url]http://wiki.garrysmod.com/?title=Entity.SetModel[/url] <-- We use Entity.SetModel because a player is an entity
[url]http://wiki.garrysmod.com/?title=Hook.Add[/url]
[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 :
[code]player.GetByID(1):SetModel("path/to/model.mdl") -- Change that to a valid model [/code]
[url]http://wiki.garrysmod.com/?title=Player.GetByID[/url]
[code]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[/code]
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]
[url]http://wiki.garrysmod.com/?title=G.RunConsoleCommand[/url]
[url]http://wiki.garrysmod.com/?title=Concommand.Add[/url]
For text use this :
[url]http://wiki.garrysmod.com/?title=Panel.AddText[/url]
and you should probably take a look at the library :
[url]http://wiki.garrysmod.com/?title=Panel[/url]
Thank you
Sorry, you need to Log In to post a reply to this thread.