setting model

Hi! im wanting to save a players model that they choose in a menu using meta and that and setting it, this doesnt seem to work, can someone point me in the right direction?

my meta file:



local meta - FindMetaTable("Player")
function meta:SetPModel(model)
	self:SetNetworkedString( "PModel", model )
	self:SavePModel()
end
 
function meta:SavePModel()
	local PModel = self:GetPModel()
	self:SetPData("PModel", PModel)
end
 
function meta:GetPModel()
	return self:GetNetworkedString( "PModel" )
end


setting their model: ( I Have A Table Of Around 100 Random Models Aswell So…



function SetClothes( ply )
local PModels = ply:GetPModel()
local ChosenPModel = math.random( DefaultModels )

	if PModels == nil then
		ply:SetPModel( ChosenPModel )
		ply:SetModel( PModels )
		ply:SavePModels()
	else
		ply:SetModel( PModels )
	end
end
hook.Add( "PlayerInitialSpawn", "settingClothes", SetClothes )


What doesn’t work? If you join the server the “PModel” NWString doesn’t exist, you need to hook.Add( “PlayerInitialSpawn” and from there get his PData, and put in the NWString.
And about setting if his PModel is nil, not sure if math.random( table ) works, try table[ math.random( 1, #table ) ].

ive already added a hook (bottom of setting their model)

[editline]11th May 2015[/editline]

also it brings up this error:



[ERROR] gamemodes/gt/gamemode/init.lua:795: attempt to call method 'GetPModel' (a nil value)
  1. fn - gamemodes/gt/gamemode/init.lua:795
   2. unknown - addons/ulib/lua/ulib/shared/hook.lua:108


Yes, which calls this function:
function meta:GetPModel()
return self:GetNetworkedString( “PModel” )
end

You need to, on InitialSpawn, get his saved model GetPData, if its nil then give him a random model, once he picks a model you SetPData.



function SetClothes( ply )
local CurModel = ply:GetPData("PModel")
	if CurModel == nil then
		ply:SetModel( DefaultClothes[1] )
	else
		ply:SetModel( CurModel )
     end
end
hook.Add( "PlayerInitialSpawn", "settingClothes", SetClothes )


like that?

yes

ok il see if that works…

[editline]11th May 2015[/editline]

yeah thanks that worked, thanks for guiding me on that one!

[editline]11th May 2015[/editline]

ok it doesnt save on disconnect, ive added it to save but says its a nil value so would i beable to do this…



function SetClothes( ply )
local CurModel = ply:GetPData("PModel")
	if CurModel == nil then
		ply:SetModel( DefaultClothes[1] )
			print("   <<< Creating New Player Character >>>")
			print("  <<<< New Player Character Created  >>>>")
				ply:SetPModel( CurModel )
	else
		ply:SetModel( CurModel )
		print("   <<< Loading Player Character >>>")
		print("  <<<< Player Character Loaded  >>>>")
	end
end
hook.Add( "PlayerInitialSpawn", "settingClothes", SetClothes )


^^ also ive added a print function to notify me through console

[editline]11th May 2015[/editline]

never mind i fixed it, heres my code for anyone intrested



hook.Add( "PlayerDisconnected", "SaveModel", function( ply )
	ply:SetPData("PModel", ply:GetModel())
	print(ply:Nick() .. " Disconnected Saving Model To SQLite")
end )

function SetClothes( ply )
local DefModel =  table[ math.random( 1, #DefaultClothes ) ]
local CurModel = ply:GetPData("PModel")
	if CurModel == nil then
		ply:SetModel( DefModel )
			print("   <<< Creating New Player Character >>>")
			print("  <<<< New Player Character Created  >>>>")
				ply:SetPData("PModel", DefModel ) 
	else
		ply:SetModel( CurModel )
		print("   <<< Loading Player Character >>>")
		print("  <<<< Player Character Loaded  >>>>")
	end
end
hook.Add( "PlayerInitialSpawn", "settingClothes", SetClothes )