Tested and works.
Well here is the code:
Server side
-- replace the old function myskins with the new one i found some bugs in old one.
--And then add the new event "ChangeSkin"
function myskins(thePlayer,commandName)
if thePlayer then
local acc = getPlayerAccount( thePlayer )
local acc_name = getAccountName( acc )
local q = dbQuery(connection,"SELECT username,skin FROM skins WHERE username=?",tostring(acc_name))
local rezult = dbPoll(q,-1)
if #rezult > 0 then
triggerClientEvent(thePlayer,"skin_inventory",thePlayer,rezult)
end
end
end
addCommandHandler("myskins",myskins,false,false)
addEvent("ChangeSkin",true)
change_skin = function(Player,skin)
if source == Player and client == source then
setElementModel(Player,skin)
end
end
addEventHandler("ChangeSkin",getRootElement(),change_skin)
Client side
-- just replace all the code with this there are few changes but maybe u insert them wrong or not so u better replace all the code with this.
function centerWindow (center_window) --- a function to put the window in the center of the screen
local screenW, screenH = guiGetScreenSize()
local windowW, windowH = guiGetSize(center_window, false)
local x, y = (screenW - windowW) /2,(screenH - windowH) /2
return guiSetPosition(center_window, x, y, false)
end
addEvent("skin_inventory",true) -- we add the skin_inventory event client side so we can call it from server side
function skin_inventory_gui(rezult) -- here we have our table sent from server to client
skin_list = guiCreateWindow(329, 246, 465, 381, "MY SKINS", false)
centerWindow(skin_list)
guiWindowSetMovable(skin_list, false)
guiWindowSetSizable(skin_list, false)
skins = guiCreateGridList(9, 20, 376, 351, false, skin_list)
guiGridListAddColumn(skins, "Description", 0.5)
guiGridListAddColumn(skins, "Model/Skin Owned", 0.5)
close = guiCreateButton(389, 20, 66, 21, "X", false, skin_list)
set_skin = guiCreateButton(389, 40, 66, 21, "SET SKIN", false, skin_list)
guiSetProperty(close, "NormalTextColour", "FFAAAAAA")
showCursor(true)
for key, value in ipairs(rezult) do -- we loop through table
local row = guiGridListAddRow(skins) -- so for each result we get we insert a row with
guiGridListSetItemText (skins, row, 1, "Skin Model ->", false, true) -- this value
guiGridListSetItemText (skins, row, 2, value.skin, false, true) --- and skin model / id
end
addEventHandler ( "onClientGUIClick", close, closeinventory,false) -- event for close function and button!!!
addEventHandler ( "onClientGUIClick", set_skin, closeinventory,false) -- event for set_skin button !!!
end
addEventHandler("skin_inventory",root,skin_inventory_gui)
closeinventory = function(button,state) --- function to close the gui window and change the skin
if (button == "left") and (state == "up") then
if source == set_skin then
if (guiGridListGetSelectedItem (skins)) then
local skin_to_change = guiGridListGetItemText (skins, guiGridListGetSelectedItem (skins), 2)
triggerServerEvent("ChangeSkin",localPlayer,localPlayer,skin_to_change)
outputChatBox("[SKIN SYSTEM] You have changed your skin",160,255,160)
end
end
showCursor (false)
guiSetVisible(skin_list,false)
end
end