Get the skin in DModelPanel?

I’m attempting to make a NPC that will change your playermodel’s skin / bodygroup, and I was wondering how I would get the DModelPanel’s skin?

Sorry if I didn’t explain it correctly. If it helps, I want to do something like this: DModelPanel:GetSkin.

DModelPanel:GetEntity():GetSkin()

Wow, I really am the biggest idiot sometimes. Thank you very much, though! :smile:

[editline]23rd July 2016[/editline]

I’ve run into another issue that I can’t solve…

I’m trying to set the player’s skin when I click on a DButton. However, when I click the DButton it changes the skin and then immediately changes back to the default skin. How would I set the skin semi-permanently?

Here’s my cl_init.lua code:


include("shared.lua")

function ShopMenu()
	local ShopParent = vgui.Create("DFrame")
  	ShopParent:SetSize( 350, 350 )
		ShopParent:SetPos( ScrW() / 2 - (350 / 2), ScrH() - 360 )
  	ShopParent:SetTitle('Clothing Shop')
  	ShopParent:SetSizable( false )
  	ShopParent:SetDeleteOnClose( true )
		ShopParent:SetDraggable( false )
  	ShopParent:MakePopup()

  local ShopPlyModel = vgui.Create( "DModelPanel", ShopParent )
    ShopPlyModel:SetSize( 300, 300 )
    ShopPlyModel:Center()
    ShopPlyModel:SetModel( LocalPlayer():GetModel() )
		ShopPlyModel.Entity:SetSkin( LocalPlayer():GetSkin() )

	local ShopButtonRight = vgui.Create( "DButton", ShopParent )
		ShopButtonRight:SetPos( ShopParent:GetWide() - 65, ShopParent:GetTall() / 2 - 30 )
		ShopButtonRight:SetText( ">" )
		ShopButtonRight:SetSize( 60, 60 )
		ShopButtonRight.DoClick = function()
			ShopPlyModel.Entity:SetSkin( ShopPlyModel:GetEntity():GetSkin() + 1 )
	  end

	local ShopButtonLeft = vgui.Create( "DButton", ShopParent )
		ShopButtonLeft:SetPos( 5, ShopParent:GetTall() / 2 - 30 )
		ShopButtonLeft:SetText( "<" )
		ShopButtonLeft:SetSize( 60, 60 )
		ShopButtonLeft.DoClick = function()
			ShopPlyModel.Entity:SetSkin( ShopPlyModel:GetEntity():GetSkin() - 1 )
	  end
	local ShopButtonBuy = vgui.Create( "DButton", ShopParent )
		ShopButtonBuy:SetPos( ShopParent:GetWide() / 2 - 30, ShopParent:GetTall() - 35 )
		ShopButtonBuy:SetText( "Buy" )
		ShopButtonBuy:SetSize( 60, 30 )
		ShopButtonBuy.DoClick = function( ply )
			LocalPlayer():SetSkin( ShopPlyModel:GetEntity():GetSkin() )
		end
end

usermessage.Hook("ShopNPCUsed", ShopMenu)


Because you are changing the players model clientside, and it should be called serverside for it to work properly, beacouse if you change the model of a player clientside, then the server will send back which player model that player has and set it to the original, so in this case you should use net to be abo

I get easily confused when using the net libraries… what exactly would I send to the server and what would the server handle?

Would be something like this:
Just remember, in the server NEVER trust the client, do as much checks as you can so the client cannot do unintended things like for example using a skin without even having it bought/having enough money to buy it.



-- PANEL PART
ShopButtonBuy.DoClick = function(btn)
	net.Start("ShopSkinChanger")
		net.WriteFloat(ShopPlyModel:GetEntity():GetSkin())
	net.SendToServer()
end


-- SERVERSIDE
util.AddNetworkString("ShopSkinChanger")

net.Receive("ShopSkinChanger", function(ply, len)
	local skinid = net.ReadFloat()
	if skinid <= (ply:SkinCount() or 0) then
		ply:SetSkin(skinid)
	end
end)


I tried what you put in, but it does the same thing.

You are clearly not, beacouse that should work.
As i already said, you have to adapt it, not to copy - paste it.
First of all, did you replace the ShopButtonBuy.DoClick with mine?
2nd, did you put the code that is after the SERVERSIDE comment in a serverside lua file?

Yeah, I put them both in. What am I doing wrong?

I put this for the ShopButtonBuy:


	local ShopButtonBuy = vgui.Create( "DButton", ShopParent )
		ShopButtonBuy:SetPos( ShopParent:GetWide() / 2 - 30, ShopParent:GetTall() - 35 )
		ShopButtonBuy:SetText( "Buy" )
		ShopButtonBuy:SetSize( 60, 30 )
		ShopButtonBuy.DoClick = function(btn)
			net.Start("ShopSkinChanger")
				net.WriteFloat(ShopPlyModel:GetEntity():GetSkin())
			net.SendToServer()
		end

And I put this in my init.lua…


util.AddNetworkString("ShopSkinChanger")

net.Receive("ShopSkinChanger", function(ply, len)
	local skinid = net.ReadFloat()
	if skinid <= (ply:SkinCount() or 0) then
		ply:SetSkin(skinid)
	end
end)

Do I have to change the code around a bit for it to work?

That should work, try restarting the server i guess…
Are you sure that code is loading?
Try putting some prints around it to see if it loads.

Restarting the server didn’t work. Putting prints around it didn’t seem to print anything either.

Do I need to put the net.Receive in a certain function for it to load properly?

No, you can just put it outside of everything.
If the prints dont work that means that the init.lua file is not loading.
Are you sure you have included it somewhere?

Yes, the init.lua is setup properly.

Try putting a print inside the function of net.Receive and the press the button on the client and take a look to the servers console to see if something prints.
Also, what gamemode are you in?

I found an error in the server’s console:


Lua Error: [ERROR] gamemodes/ultimaterp_remake/gamemode/init.lua:17: attempt to index local 'ply' (a number value) 1. func - gamemodes/ultimaterp_remake/gamemode/init.lua:17 2. unknown - lua/includes/extensions/net.lua:32

Oh fuck, in the net.Receive arguments i put the arguments like this


function(ply, len)

when it should be


function(len, ply)[(CODE]

My fault :sick:

Yup, that fixed it. Thank you so much! :goodjob: