Jump to content

elementData


Klesh

Recommended Posts

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.

Link to comment

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) 

Link to comment

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 ) 

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