Random model on loadout (Gamemode LUA question)

I’ve been messing around with a gamemode, more for fun than anything else.

I added the following script to my GM:PlayerLoadout function in init.lua


modelGender = math.random(0,1)
        if (modelGender == 0)
        then
            modelNumber = math.random(1,9)
            modelGenders = "male_0"
        end
        if (modelGender == 1)
        then
            modelNumber = math.random(1,7)
            modelGenders = "female_0"
        end
        ply:SetModel( "models/player/Group1/"..modelGenders..modelNumber ) --Set the players model
        Msg("Model: "..modelGenders..modelNumber)

The idea being it would first randomly assign the player as male or female. Then, it would pick a model (1-7 for female, 1-9 for male) from the Group1 folder of the player models, randomly. The Msg was just for debugging purposes. The message displays a valid, random model every spawn. Do I have to parse the int “modelNumber” as a string then combine it with “models/player/Group1/” and “modelGender” first?

Thanks, Jeremy

Edit: Realized I didn’t add “.mdl” to the end. Fixed it and made a small modification, but it still doesn’t work



         ply:Give("weapon_crowbar")
        modelGender = math.random(0,1)
        if (modelGender == 0)
        then
            modelNumber = math.random(1,9)
            modelGenders = "male_0"
        end
        
    
        if (modelGender == 1)
        then
            modelNumber = math.random(1,7)
            
            modelGenders = "female_0"
        end
        
    pModel=("models/player/Group1/"..modelGenders..modelNumber..".mdl")
    ply:SetModel(pModel) --Set the players model
    Msg("Model: "..pModel)



This is what is sent to the console:

Model: models/player/Group1/male_05.mdl

(it changes every respawn)

Are there any errors?

Also, if you suicide once after the initial connect, does it work or no?

Use your code in the **[Gamemode.PlayerSetModel

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Gamemode.PlayerSetModel)** hook.

No errors. If I suicide, it generates a new model but it still doesn’t work.

No idea what that is, I will take a look and post back with results.

Thanks for the replies!

EDIT 1:
Ok, I followed that link and attempted to use GM:PlayerSetModel. Upon running the script again, I now get this error:




] kill
Jer. suicided!
ERROR: GAMEMODE:'PlayerLoadout' Failed: gamemodes	est\gamemode\init.lua:19: attempt to index global 'GM' (a nil value)


I google’d it and found another thread on facepunch where someone had a similar problem http://www.facepunch.com/showthread.php?t=803601 .

Somehow he was able to get the error to go away (although he never got the models to change). I altered the code in 12 different ways and still end up with the error.

Here is the current version:



function GM:PlayerSetModel( ply )
        
            local modelGender = math.random(0,1) //male or female
            local modelNumber //Which model (1-9 for male, 1-7 for female)
            local modelGenders //The string version of male or female
            local pModel //The full string containing path, model, and extention
            
            if (modelGender == 0) then
                modelNumber = math.random(1,9)
                modelGenders = "male_0"
            end
        
            if (modelGender == 1) then
                modelNumber = math.random(1,7)
                modelGenders = "female_0"
            end
            pModel=("models/player/Group1/"..modelGenders..modelNumber..".mdl")
            util.PrecacheModel( pModel )
            ply:SetModel(pModel) 
        end


EDIT 2:
After many many failed attempts… it finally works.



function GAMEMODE:PlayerSetModel( ply )
        
            local modelGender = math.random(0,1) //male or female
            local modelNumber //Which model (1-9 for male, 1-7 for female)
            local modelGenders //The string version of male or female
            local pModel //The full string containing path, model, and extention
            
            if (modelGender == 0) then
                modelNumber = math.random(1,9)
                modelGenders = "male_0"
            end
        
            if (modelGender == 1) then
                modelNumber = math.random(1,7)
                modelGenders = "female_0"
            end
            pModel=("models/player/Group01/"..modelGenders..modelNumber..".mdl")
            util.PrecacheModel( pModel )
            ply:SetModel(pModel) 
            Msg("Model: "..pModel)
        end


I honestly don’t know exactly what did it. I had changed GM to GAMEMODE a while ago, and that alone didn’t work. After messing around with several more things and un-doing them… I don’t know. But it works great.

Wait what? It should be GM normally? xD

[lua]
PlayerModels = {
“Put”,
“All”,
“Your”,
“Models”,
“Here”
}

for k,v in pairs( PlayerModels ) do
util.PrecacheModel( v )
end

function GM:PlayerSetModel( ply )

ply:SetModel( table.Random( PlayerModels ) );

end
[/lua]

It looks nice and small on here, but I would have to list out 31 different models in that table.

Thanks though

I’m guessing you were having the GM/GAMEMODE trouble because you aren’t making a gamemode. Use a hook instead.

**[Hook.Add

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Hook.Add)**

But I am making a gamemode…