[Release] PlayerVar ~ Attach Variables to players?

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!


https://github.com/LexTheGreat/PlayerVar/archive/master.zip
Install this like any other resource.

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

PS: Everything SHOULD work… should :game_die:

2 Likes

can i have a exmple of what the guid would look like in the admin array

Essential mode already propose that actually. User:setSessionVar(“myVar”, myValue)

Hey, i using your script but i got error

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')

this error in

local Player = PlayerVar.Player[source]

I have really no idea where you’ve found local players, please Quote it, couldn’t find it.

this one

You sure you did this?
because

Means that the function you try to trigger couldn’t get found / triggert.

Make sure that your file in __resource.lua is registered as a server_script. It will not work in a client script.
@nurfaizfy19

PlayerVarAPI.lua right?

Not sure what you mean. But this needs to get pasted in your server.lua if I’ve understand that correctly.

Edit: and make sure to not paste this:

Only the stuff after the

ex:

Okay, i will try again later :sleepy:. If I restart the resource will the variable be lost?

Oh man, still not working for me. I try this one

function setElementData(var, val)
	local data = PlayerVar.Player[source]
	data:SetVar(var, val) -- Line 3
end
AddEventHandler("setElementData", setElementData)

and i have put this like you say

server_script 's_pool.lua'
client_script 'c_pool.lua'
server_script '@PlayerVar/server/PlayerVarAPI.lua' -- PlayerVar script

server_export "setElementData"
server_export "getElementData"

export "setElementData"
export "getElementData"

and got this error

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')

Try to replace

With data.SetVar

And what do you try to get? On that way I can help you much more then only See a small part of your code and the errors, that you get.

Edit:

And you need to set a variable and a value at (var, val) without that, it will not work as expected.

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

i use exports.pool:setElementData(var, val) to set the variable and value

ERROR ERROR ERROR