Setting player model

I have tried and tried to set a special player model for only super admins, i have tried different methods but i have gotten multiple timer errors and other unrelated errors in my code…is there a way i can make the player take a custom model depending on there rank?

Lua snippit please with the code needed to set say a basic non custom player model for a super admin on spawn all the time :slight_smile:

Thanks a lot its had me stumped for a while

[lua]
for _,ply in pairs(player.GetAll())
if ply:SteamID() == SteamIDhere then
ply:SetModel(Modelpathhere)
break
end
end
[/lua]

I use the following on my server, works fine:

[lua]
local playerinformation = {
[“xxx1”] = {model = “models/player/psycho/cherry_2.mdl”, r = 255, g = 255, b = 255, a = 255 },
[“xxx2”] = {model = “models/slow_alien.mdl”, r = 57, g = 255, b = 0, a = 255 },
[“xxx3”] = {model = “models/player/hczombie.mdl”, r = 255, g = 255, b = 255, a = 255 },
}

hook.Add( “PlayerLoadout”, “Spawnage”, function( p )
local playerinfo = playerinformation[p:SteamID()]
if p:IsValid() then
if playerinfo then
p:SetModel(playerinfo.model)
if playerinfo.r then
p:SetColor(playerinfo.r, playerinfo.g, playerinfo.b, playerinfo.a)
end
end
end
end)
[/lua]

PlayerSpawn instead of PlayerLoadout could work?

Yeah probably, just used Loadout to ensure it’s run after any playerspawn setmodel things.

Thanks guys, very useful indeed!

Is there an edit or an extension to that code to also include groups? Like guest and admin groups? (default)

This isn’t the request section

For the default Admin, Super Admin and non-admin groups, you could use this:
[lua]
–Place this into init.lua if you are editing/making the gamemode, otherwise somewhere in your addon
function GroupModels(ply)

if ply:IsAdmin() == false then
	ply:SetModel("models/player/group01/male_01.mdl")
	
elseif ply:IsAdmin() == true and ply:IsSuperAdmin() == false then
	ply:SetModel("models/player/police.mdl")
	
elseif ply:IsSuperAdmin() == true then
	ply:SetModel("models/player/combine_super_soldier.mdl")
	
end

//return true --Uncomment in if you are putting into an addon, not making/editing the gamemode

end

hook.Add(“PlayerLoadout”, “GroupModels”, GroupModels)
[/lua]
If you are using custom groups then you would need to make your own
[lua]
function Player.IsWhateverGroup(ply) --Whatever the group is (Not sure about the name of this function. To facepunch: if you use Player. at the start of a function, is that how you are able to do ply:Function?
[/lua]
function. You would create a table of the group members and then use
[lua]
table.HasValue(WhateverGroupTable, ply:SteamID)
[/lua]
to check if he is in the table. Then you would return true if he is, and false if he isn’t.
Then in the model function you would do
[lua]
if ply:IsWhateverGroup() == true then
ply:SetModel(“models/player/combine_soldier.mdl”)
[/lua]
I am a noob too, so get this code checked, and I have a question written in the comments.

You don’t need to do == true, and instead of == false just use negation (!ply:IsAdmin()).

And can I name the function Player.IsModerator(ply) and later use it as ply:IsModerator(), or should I name it IsModerator(ply) and use it the same way?

function _R.Player:IsModerator()

You have to access the meta table.

What do you mean?
The code I think is right:
[lua]
ModTable = {MyID, MyFriendsID, AnotherMod’sID}

function Player.IsModerator(ply)
if table.HasValue(ModTable, ply:SteamID()) then
return true
else
return false
end
end
[/lua]

[editline]26th March 2011[/editline]

Later it will be used as
[lua]
function ModeratorModels(ply)
if ply:IsModerator() then
ply:SetModel(“models/player/combine_soldier.mdl”)
end
end
[/lua]
is this right?

To add to what Drew told you, just do
[lua]
function _R.Player:IsModerator()
return table.HasValue(ModTable, self:SteamID())
end
[/lua]

then you can do ply:IsModerator()

[lua]
local function GroupModels(ply)
if ply:IsSuperAdmin() then
ply:SetModel(“models/player/combine_super_soldier.mdl”)
elseif ply:IsAdmin() then
ply:SetModel(“models/player/police.mdl”)
else
ply:SetModel(“models/player/group01/male_01.mdl”)
end
end
hook.Add(“PlayerLoadout”,“GroupModels”,GroupModels)
[/lua]

Here’s the code you wrote above structured better, checking it in a different order makes it more efficient.

So if you check ply:IsAdmin() after ply:IsSuperAdmin(), it wont set Super Admin’s model twice? Because ply:IsAdmin() applies to S-Admins and normal Admins.

It won’t go any further if they are a super admin.