Best way to make custom variables for players on server script

hello, Is there a way you can create variables for all players on a server script and modify them separately depending on the actions of the player?

you can do this with remote events using

local Event ="RemoteEvent")


and can have the server use it like this

Event.OnServerEvent:Connect(function() -- replace event with how ever you get ur event
1 Like

I have my 2 variables on the server and i want to access them from the server but I want there to be those same variables for other players that join.

yea it will still work , so basicly fire the server through a remote event and have the server script hold the variable

let me make a example , give me a minute

ok, this is confusing me. I thought you couldn’t fire a remote event from server to server

Oh if u want it from server to server use a bindable event

so from the sending server script you would say


and fromt the reciving one you would do


ok, but i still don’t understand how i get variables to start with because they need to be cloned for every player that joins. I have tried to use values in server storage but this made my event only fire once.

Oh you can do

local Variable1
local Variable2

Yes, just use tables to store it

local t = {} -- Use RemoteFunctions if you want to send this table to the client.

    t[player] = "your value here" -- by using the player as index we make it unique for each player
    -- I'd recommend you to use the player's id

If i was to store multiple variables inside of this table would I have to do var1 = 1, var2 = 2 etc for "your value here"?

I, personally, would just use session data. Basically, I’d use a modulescript which includes a function to add information to a table. I’ll give a quick example.

local SessionData = {}

function SessionData.get(player)
    return SessionData[player]

function SessionData.set(player, data)
    SessionData[player] = data

function SessionData.remove(player)
    SessionData[player] = nil

return SessionData

Now, from a serverscript, I could just do:

local SessionData = require(path/to/sessiondata)

    SessionData.set(player, {1, 2})

-- Now, I can easily use the `SessionData.get()` function to get the data!

print(SessionData.get(selectedPlayer)) -- Will print whatever data they have saved, in this case `{1, 2}`

if you want to store multiples values you can do this:

table[player] = {} -- attach another table and insert all your values

-- example
table[player] = {
    Cash = 0

-- modifying
if table[player] then
   table[player].Cash += 500

-- accessing
print(table[player].Cash) -- 500 

Just save a table using my sessiondata example I gave. It’ll work nicely, and won’t need the use of BindableEvents.

so if i was to get variables from your example I would have to do SessionData.get(selectedPlayer).Varaible ?

If you saved a table to the SessionData table under the player key, then yes.
So, if you saved in

{ Variable = 1, Variable2 = 2, Variable3 = 3 }

then you would just do:


Can you give me an example on what you need that for?

For adjusting a players ‘fake’ health from the server. I say fake because it’s a variable and I’m not actually changing the players current hp. I want to keep specific things server sided to make it easier.

I would use a dictionary for that.

local Players = game:GetService("Players")
local fakeHealths = {}

    fakeHealths[plr.Name] = 100

    fakeHealths[plr.Name] = nil