StateBag - pitermcflebor/fivem-classes-lua GitHub Wiki

Init Client-side / Server-side

local statebag = StateBag(entityId) -- local entity id

Methods

StateBag():set(key, value, shared) -- shared is optional
StateBag():get(key)
StateBag():clearAll()
StateBag():clear(key)

Example

Client-side

RegisterCommand('createcar', function()
	-- get the player
	local player = CPlayer(-1)
	-- get the player position
	local pos = player:GetPosition()
	-- create the vehicle
	local vehicle = Vehicle(true, 'cheetah', pos.x, pos.y, pos.z, pos.w, true)
	-- set player inside the vehicle
	vehicle:SetPedInside(player.ped, -1)
	-- create a statebag for the vehicle created
	local statebag = StateBag(vehicle.id)
	-- set a new key 'test' with value 'true' and share it
	statebag:set('test', true, true)
	-- set a new key 'testnotshared' with value 'true' and DONT share it
	statebag:set('testnotshared', true, false)
end, false)

RegisterCommand('getcar', function()
	-- get the player
	local player = CPlayer(-1)
	-- get the player vehicle
	local vehicle = player:GetVehicle()
	-- get the statebag of the vehicle
	local statebag = StateBag(vehicle.id)
	-- get the key 'test'
	print('test', statebag:get('test'))			-- prints 'test true'
	print('testnotshared', statebag:get('testnotshared'))	-- prints 'testnotshared true'
end, false)

Server-side

RegisterCommand('getcarsv', function(source)
	-- get the player
	local player = CPlayer(source)
	-- get the vehicle
	local vehicle = player:GetVehicle()
	-- get the statebag
	local statebag = StateBag(vehicle.id)
	-- get the key 'test'
	print('test', statebag:get('test'))			-- prints 'test true'
	print('testnotshared', statebag:get('testnotshared'))	-- prints 'testnotshared nil'
end, false)