Skip to content

throwarray/fivem-localstorage

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

localStorage

  • isReady(): boolean

  • hasItem (k): boolean

  • setItem (k, v): string

  • getItem (k): string | void

  • removeItem(k): string | void

  • key (i): string | void

  • dump (): string | void

  • getLength (): integer

  • clear (): void

Example

Store and retrieve the player position

local localStorage = nil
local coords = nil
local function echo (msg) print(msg) end

local function updatePosition ()
    TriggerEvent('localstorage:getSharedObject', function (obj)
        localStorage = obj
    end)

    while not localStorage do
        Citizen.Wait(0)
    end

    coords = localStorage.getItem('position')

    if coords ~= nil then
        echo('Update player position')
        coords = json.decode(coords)
        SetEntityCoords(
            GetPlayerPed(-1),
            coords.x,
            coords.y,
            coords.z - 1
        )
    end
end

local function savePosition ()
    echo('Save player position')
    coords = GetEntityCoords(GetPlayerPed(-1))
    localStorage.setItem('position', json.encode({
        x = coords.x;
        y = coords.y;
        z = coords.z;
    }))
end

AddEventHandler('playerSpawned', function ()
    echo('Player spawned')
    updatePosition()
end)

Citizen.CreateThread(function ()
    updatePosition() -- handle restart
    while true do
        Citizen.Wait(10000)
        savePosition()
    end
end)

LICENSE MIT

MDN - Using the Web Storage API

Releases

No releases published

Packages

No packages published