Lalalu Posted February 26, 2023 Share Posted February 26, 2023 Hello everyone. I am creating a skins store, but I would like to know how I can save each skin I buy in a list, as items since there are many skins. Is there a way to create a save system for each skin? saving it by id? I don't know if I explain myself well. Link to comment
FlorinSzasz Posted February 27, 2023 Share Posted February 27, 2023 Well first of all u need a place so save data u can use setAccountData or dbConnect() dbExec() if u want to go for setAccountData u dont need the others but if u know how to use a database than u can go for the other ones. I will show u later an example if u need i am at work now Link to comment
FlorinSzasz Posted February 27, 2023 Share Posted February 27, 2023 So here is the code Server Side local connection = dbConnect("sqlite","skins.db") -- we create the db our is called skins.db u can call it any way u want if connection then -- check for connection outputDebugString("Connection with SKIN database was established") else outputDebugString("Connection with SKIN database was not established somehting went wrong") end function db_table() -- create the table in db dbExec(connection,"CREATE TABLE IF NOT EXISTS skins (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,username TEXT,skin INTEGER)") end addEventHandler("onResourceStart",getResourceRootElement(),db_table) -- skin cost -- local cost = 1000 ---- ----- addCommandHandler("buyskin",function(thePlayer,commandName,model) if thePlayer then -- check for player local acc = getPlayerAccount( thePlayer ) -- get his account name local acc_name = getAccountName( acc ) -- get his account name if model and tonumber(model) >= 0 and tonumber(model) <= 84 then -- i choose to allow to buy the skins from model 0 to 84 u can change it the way u want also if u dont provide a model u get an message local q = dbQuery(connection,"SELECT username,skin FROM skins WHERE username=? AND skin=? LIMIT 1",tostring(acc_name),tonumber(model)) -- we select the data from to see if we have this skin already local rezult = dbPoll(q,-1) if #rezult > 0 then --- if we have rezult then we loop throgh rezult and check for key,value in ipairs(rezult) do -- loop if tonumber(value.skin) == tonumber(model) then -- check if we have this skin outputChatBox("[SKIN_SYSTEM] You have this skin already",thePlayer) end end else if getPlayerMoney(thePlayer) >= cost then dbExec(connection,"INSERT INTO skins (username,skin) VALUES (?,?)",tostring(acc_name),tonumber(model)) -- if player has more money or enough then we buy the skin and insert the data in db and take his money takePlayerMoney(thePlayer,cost) outputChatBox("[SKIN SYSTEM] You bought a new skin!",thePlayer,104,255,104) elseif getPlayerMoney(thePlayer) < cost then outputChatBox("[SKIN SYSTEM] You don have enough money to buy the skin",thePlayer) end end else outputChatBox("[SKIN_SYSTEM] Please provide the model u want to buy!",thePlayer) -- provide model message end end end) function myskins(thePlayer,commandName) if thePlayer then --- again check for player local acc = getPlayerAccount( thePlayer ) local acc_name = getAccountName( acc ) local q = dbQuery(connection,"SELECT username,skin FROM skins WHERE username=?",tostring(acc_name)) -- we select the data from db, by data i mean his skins and then send the table on client local rezult = dbPoll(q,-1) if #rezult > 0 then for key,value in ipairs(rezult) do triggerClientEvent(thePlayer,"skin_inventory",thePlayer,rezult) -- if we have rezult then we call client event skin_inventory!! end end end end addCommandHandler("myskins",myskins,false,false) Client Side 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) 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!!! end addEventHandler("skin_inventory",root,skin_inventory_gui) closeinventory = function(button,state) --- function to close the gui window if (button == "left") and (state == "up") then showCursor (false) guiSetVisible(skin_list,false) end end So this is enough to get you started! Link to comment
Lalalu Posted April 16, 2023 Author Share Posted April 16, 2023 On 27/02/2023 at 16:46, FlorinSzasz said: So here is the code Server Side local connection = dbConnect("sqlite","skins.db") -- we create the db our is called skins.db u can call it any way u want if connection then -- check for connection outputDebugString("Connection with SKIN database was established") else outputDebugString("Connection with SKIN database was not established somehting went wrong") end function db_table() -- create the table in db dbExec(connection,"CREATE TABLE IF NOT EXISTS skins (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,username TEXT,skin INTEGER)") end addEventHandler("onResourceStart",getResourceRootElement(),db_table) -- skin cost -- local cost = 1000 ---- ----- addCommandHandler("buyskin",function(thePlayer,commandName,model) if thePlayer then -- check for player local acc = getPlayerAccount( thePlayer ) -- get his account name local acc_name = getAccountName( acc ) -- get his account name if model and tonumber(model) >= 0 and tonumber(model) <= 84 then -- i choose to allow to buy the skins from model 0 to 84 u can change it the way u want also if u dont provide a model u get an message local q = dbQuery(connection,"SELECT username,skin FROM skins WHERE username=? AND skin=? LIMIT 1",tostring(acc_name),tonumber(model)) -- we select the data from to see if we have this skin already local rezult = dbPoll(q,-1) if #rezult > 0 then --- if we have rezult then we loop throgh rezult and check for key,value in ipairs(rezult) do -- loop if tonumber(value.skin) == tonumber(model) then -- check if we have this skin outputChatBox("[SKIN_SYSTEM] You have this skin already",thePlayer) end end else if getPlayerMoney(thePlayer) >= cost then dbExec(connection,"INSERT INTO skins (username,skin) VALUES (?,?)",tostring(acc_name),tonumber(model)) -- if player has more money or enough then we buy the skin and insert the data in db and take his money takePlayerMoney(thePlayer,cost) outputChatBox("[SKIN SYSTEM] You bought a new skin!",thePlayer,104,255,104) elseif getPlayerMoney(thePlayer) < cost then outputChatBox("[SKIN SYSTEM] You don have enough money to buy the skin",thePlayer) end end else outputChatBox("[SKIN_SYSTEM] Please provide the model u want to buy!",thePlayer) -- provide model message end end end) function myskins(thePlayer,commandName) if thePlayer then --- again check for player local acc = getPlayerAccount( thePlayer ) local acc_name = getAccountName( acc ) local q = dbQuery(connection,"SELECT username,skin FROM skins WHERE username=?",tostring(acc_name)) -- we select the data from db, by data i mean his skins and then send the table on client local rezult = dbPoll(q,-1) if #rezult > 0 then for key,value in ipairs(rezult) do triggerClientEvent(thePlayer,"skin_inventory",thePlayer,rezult) -- if we have rezult then we call client event skin_inventory!! end end end end addCommandHandler("myskins",myskins,false,false) Client Side 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) 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!!! end addEventHandler("skin_inventory",root,skin_inventory_gui) closeinventory = function(button,state) --- function to close the gui window if (button == "left") and (state == "up") then showCursor (false) guiSetVisible(skin_list,false) end end So this is enough to get you started! Thank you @FlorinSzasz It works! by the way, how can I change or set my skin by clicking on item from the list? Link to comment
FLUSHBICEPS Posted April 16, 2023 Share Posted April 16, 2023 3 hours ago, Lalalu said: Thank you @FlorinSzasz It works! by the way, how can I change or set my skin by clicking on item from the list? -- clientside function setSkinFromList(button, state) if button == "left" and state == "up" then local selectedRow, selectedCol = guiGridListGetSelectedItem(skins) if selectedRow and selectedRow ~= -1 then local skinID = tonumber(guiGridListGetItemText(skins, selectedRow, 2)) if skinID then triggerServerEvent("applySkin", localPlayer, skinID) end end end end addEventHandler("onClientGUIDoubleClick", skins, setSkinFromList, false) --serverside function applySkinHandler(thePlayer, skinID) if thePlayer and skinID then setElementModel(thePlayer, skinID) end end addEvent("applySkin", true) addEventHandler("applySkin", root, applySkinHandler) Link to comment
Lalalu Posted April 17, 2023 Author Share Posted April 17, 2023 1 hour ago, FLUSHBICEPS said: -- clientside function setSkinFromList(button, state) if button == "left" and state == "up" then local selectedRow, selectedCol = guiGridListGetSelectedItem(skins) if selectedRow and selectedRow ~= -1 then local skinID = tonumber(guiGridListGetItemText(skins, selectedRow, 2)) if skinID then triggerServerEvent("applySkin", localPlayer, skinID) end end end end addEventHandler("onClientGUIDoubleClick", skins, setSkinFromList, false) --serverside function applySkinHandler(thePlayer, skinID) if thePlayer and skinID then setElementModel(thePlayer, skinID) end end addEvent("applySkin", true) addEventHandler("applySkin", root, applySkinHandler) thanks, but its not working with that 1 Link to comment
FlorinSzasz Posted April 17, 2023 Share Posted April 17, 2023 (edited) 14 hours ago, Lalalu said: Thank you @FlorinSzasz It works! by the way, how can I change or set my skin by clicking on item from the list? 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 Edited April 17, 2023 by FlorinSzasz 1 Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now