Jump to content

elementData


Klesh

Recommended Posts

Posted

Hi i try to add some code line to the script shader_nitro to save it when player quit and back nitro when player joins, so this is the whole code:

root = getRootElement() 
addEventHandler("onClientResourceStart",resourceRoot, 
function() 
    nitroShader = dxCreateShader("nitro.fx") 
end) 
  
-- This function will set the new color of the nitro 
function updateNitroColor(r,g,b) 
    if nitroShader then 
        if r and g and b then 
            engineApplyShaderToWorldTexture (nitroShader,"smoke") 
            dxSetShaderValue (nitroShader, "gNitroColor", r/255, g/255, b/255 ) 
        end 
    end 
end 
  
-- This function will reset the nitro back to the original 
function resetNitroColor() 
    if nitroShader then 
        engineRemoveShaderFromWorldTexture(nitroShader,"smoke") 
    end 
end 
  
  
  
-- Example command use 
addCommandHandler("nitro", 
function nitroPursache(command,r,g,b) 
    if r and g and b then 
        local r,g,b = tonumber(r),tonumber(g),tonumber(b) 
        if r <= 255 and g <= 255 and b <= 255 then 
            updateNitroColor(r,g,b) 
            outputChatBox("Nitro color updated!",255,255,255,true) 
        else 
            outputChatBox("Colors must be between 0 and 255",255,255,255,true) 
        end 
    else 
        resetNitroColor() 
        outputChatBox("Nitro color reset to original!",255,255,255,true) 
    end 
end) 
  
function onPlayerQuit (playerSource) 
    local acc = getPlayerAccount(source) 
    local playerVehicle = getOccupiedVehicle(playerSource) 
    if acc then 
        getPlayerAccount(nitroPursache) 
        setAccountData(acc, "nitro.data", gNitroColor(playerVehicle)) 
    end 
end 
addEventHandler("onPlayerQuit", root, onPlayerQuit) 
function onPlayerLogin () 
    if acc then 
        pNitro = getAccountData(acc, "nitro.data") 
            if pNitro then  
                setgNitroColor( source, gNitroColor ( pNitro ) ) 
            end 
    end 
end 
addEventHandler("onPlayerLogin", root, onPlayerJoin) 
         
  
         

Doesn't save, i dont know how to call nitrocolor function.

Posted

You're messing client side with server side.

I would use client side XML to save the color, but well, if you want it account data based, here it is:

-- client side:

addEventHandler("onClientResourceStart",resourceRoot, 
function() 
    nitroShader = dxCreateShader("nitro.fx") 
end) 
  
-- This function will set the new color of the nitro 
function updateNitroColor(r,g,b) 
    if nitroShader then 
        if r and g and b then 
            engineApplyShaderToWorldTexture (nitroShader,"smoke") 
            dxSetShaderValue (nitroShader, "gNitroColor", r/255, g/255, b/255 ) 
        end 
    end 
end 
  
-- This function will reset the nitro back to the original 
function resetNitroColor() 
    if nitroShader then 
        engineRemoveShaderFromWorldTexture(nitroShader,"smoke") 
    end 
end 
  
function setNitroColor(command,r,g,b) 
    if (r and g and b) then 
        local r,g,b = tonumber(r),tonumber(g),tonumber(b) 
        if (r <= 255 and g <= 255 and b <= 255) then 
            updateNitroColor(r,g,b) 
            setElementData(localPlayer,"nitroColor",{r, g, b}) 
            outputChatBox("Nitro color updated!",255,255,255,true) 
        else 
            outputChatBox("Colors must be between 0 and 255",255,255,255,true) 
        end 
    else 
        resetNitroColor() 
        outputChatBox("Nitro color reset to original!",255,255,255,true) 
    end 
end 
addCommandHandler("nitro",setNitroColor) 
addEvent("setNitroColor",true) 
addEventHandler("setNitroColor",root,setNitroColor) 

-- server side:

function onPlayerQuit (playerSource) 
    local acc = getPlayerAccount(playerSource) 
    local playerVehicle = getOccupiedVehicle(playerSource) 
    if (not isGuestAccount(acc) and playerVehicle) then 
        local nitroColor = getElementData(playerSource, "nitroColor") 
        if (nitroColor) then 
            setAccountData(acc, "nitro.data", toJSON({unpack(nitroColor)})) 
        end 
    end 
end 
addEventHandler("onPlayerQuit", root, onPlayerQuit) 
  
function onPlayerLogin (_,acc) 
    local pNitro = getAccountData(acc, "nitro.data") 
    if (pNitro) then 
        local color = getAccountData(acc,"nitro.data") 
        local r, g, b = unpack(fromJSON(color)) 
        triggerClientEvent(source,"setNitroColor",source,"",r,g,b) 
    end 
end 
addEventHandler("onPlayerLogin", root, onPlayerLogin) 

Posted

I didn't understand the toJSON event, how to use and when, can explain because wiki say's something about cats :?

Thanks, i forgot that's a server-side, and setcolorNitro, thanks for that.

Posted

Ok, i see on wiki and i read more, i understand that stores data between scripts, you use unpack to extract them on server side, thanks.

Posted

That's mean you can store data as db or SQLite, with JSON, that good to know, never seen before.(Tables)

Edit: The script doesn't save the nitro.

Posted

Try:

Server:

function onPlayerQuit ( ) 
    local acc = getPlayerAccount(soruce) 
    local playerVehicle = getPedOccupiedVehicle(soruce) 
    if (not isGuestAccount(acc) and playerVehicle) then 
        local nitroColor = getElementData(soruce, "nitroColor") 
        if (nitroColor) then 
            setAccountData(acc, "nitro.data", toJSON( { unpack( nitroColor ) } ) ) 
        end 
    end 
end 
addEventHandler( "onPlayerQuit", root, onPlayerQuit ) 
  
function onPlayerLogin (_,acc) 
    local pNitro = getAccountData(acc, "nitro.data") 
    if pNitro then 
        local r, g, b = unpack( fromJSON( pNitro ) ) 
        triggerClientEvent( source,"setNitroColor",source,"",r,g,b ) 
    end 
end 
addEventHandler( "onPlayerLogin", root, onPlayerLogin ) 

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...