Jump to content

Lalalu

Recommended Posts

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
  • 1 month later...
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
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
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 :(

  • Like 1
Link to comment
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 by FlorinSzasz
  • Thanks 1
Link to comment

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...