Clientside Variables

Hi! (:

Im making a derma menu in which information about players should be shown. When a user selects a player from a list, next time the derma opens, the same player should be selected :slight_smile:
How do i store a variable (with the selected player) on the client side? The variable should be editable on the client, and only stored on the client itself. This means CreateClientConVar() does not work and global variables can’t be adressed clientside. :frowning:

What do I do, thanks in advance :slight_smile:


local PlayerObject = nil

local variables does not work either because they are only accessible in the section they were made. E.g. a function. :confused:

Assuming your writing a gamemode, I usually just add a table to the Gamemode meta, if i need to store variables.

This has proven to be a little flaky sometimes though, and i doubt its proper.

Generally i do something like:

[lua]GM.ClientVariables = {}

GM.ClientVariables.TestVar = 1

This example would be under the client side code, then i make another for server side, appropriately named ‘ServerVariables’.

After that i set them with either ‘self.ClientVariables.x =’ or ‘GAMEMODE.ClientVariables.x =’

Otherwise, i couldn’t tell you how to go about it. I don’t do a whole lot outside of gamemodes.

If your code is something like:
[lua] function DrawDermaOnClient()
ShowStats(selectedPlayer) --A function that shows the stats of the player previously selected[/lua]
then all you need is to declare
[lua] local selectedPlayer[/lua]
BEFORE the previous statement. This will allow you to mantain the state of “selectedPlayer” between excecutions of DrawDermaOnClient(), because in LUA any local variable is defined in any level lower and deeper than where it was declared. Reffer to the LUA documentation: “Variable Scopes” for more info.

local A = “Hello”
function AandB()
local B = “Bob”
print ( A … " " … B) – Prints “Hello Bob”
function OnlyA()
print( A ) – prints “Hello”, once more
print( B ) – will give error as B is not declared in this scope because its local to aTOb()

[lua] local storedPly = nil

function StorePlayer( ply )
if storedPly == nil then
storedPly = ply
print(“There is a stored player already!”)

function DrawPlayer()
if storedPly == nil then
print(“There’s nobody there!”)
local temp = storedPly
storedPly = nil
return temp

DrawPlayer() – prints “There’s nobody there!”
StorePlayer( LocalPlayer() ) – stores the local player in the variable
StorePlayer( some_random_player ) – prints “There is a stored player already!”
DrawPlayer() – returns LocalPlayer()