Hello! There is prob a better way to do this (Not sure if there is something like exports for the server?). But I was bored… So I made this. My next few resources released will use this as It’s back bone.
This is only for server side resources at the moment. I plan to finish it so clients can sync the array with the server as well.
This is a API that allows you to create plugins that need to save variables to players. Or if you just need to get the name of a player or id!
Inside the server folder is a PlayerVarAPI.lua.
If you want to create a resource that uses this check this file out.
You need to add this to your server_scripts if you plan to use this file.
Important Functions/Var
Player.Name > Name
Player.Id > ID from source
Player:SetVar(Name, Var)
Player:GetVar(Name)
Ex:
local Player = PlayerVar.Players[source]
Player:SetVar("Score", 100)
local Score = Player:GetVar("Score") > 100
Error running call reference function for resource pool: citizen:/scripting/lua/scheduler.lua:403: @pool/server.lua:88: attempt to index a nil value (global 'Player')
function setElementData(var, val)
local data = PlayerVar.Player[source]
data:SetVar(var, val) -- Line 3
end
AddEventHandler("setElementData", setElementData)
cfx> Error running system event handling function for resource pool: citizen:/scripting/lua/scheduler.lua:41: Failed to execute thread: @pool/s_pool.lua:3: attempt to index a nil value (local 'data')
I have not used PlayerVar in ages, not sure if it still works even (Have not updated in a while)… But from your code, the variable “source” is undefined. setelementData needs to be setElementData(source, var, val) you need to use the players ID to get the player data from that player
function setElementData(player, var, val)
local data = PlayerVar.Player[player]
data:SetVar(var, val)
end