Same code for everyone on the server?

I’ve been working on a small event, and I wanted to create a different code for everyone that joins, but I came across an issue: Everyone that joins the same server gets the same code.

here is the top part of my code:


local Datastoreservice = game:GetService(“DataStoreService”)
local Datastore = Datastoreservice:GetDataStore(“12”)

local ServerUserData = {}

local StatsDictionary = {
	UserCode = nil;
	HasKey = false

	local userdata = Datastore:GetAsync(plr.UserId)or StatsDictionary
	if userdata.UserCode == nil then
		userdata.UserCode = math.random(1000,9999)

(Let me know if you need more code, but the following code is just sending data to the client via remotes so I don’t think it is necessary.)

Basically, I wanna know if theres a way to make it so once a player joins, he/she gets a unique 4 digit code not shared by anyone.


Have a bunch of modules and require a random one everytime player joins the game if that’s what you want?

I don’t think that would be a decent way of getting a 4-digit code. With the code provided, I need a way so that every time a player joins , the


function doesnt give that same randomized number the the next player that joins the game, and if I were to add a lot of modules, i feel like it would lag the game a bit.

Your table at the top of the script is only evaluated once, which means every player is getting the same data table when they don’t already have one. To create a new version every time, you can wrap it in a function or move it into your or expression.

Solution one:

local function NewStatsDict()
    return {
        -- your stuff here
-- later on
local userdata = blah or NewStatsDict()

Solution two:

local userdata = blah or {
    -- your stuff here

Both of these ensure you evaluate a new table every time.

Awesome that works! was a bit skeptical at first since I gave everyone the same dictionary at the start then changed them later on in the script but this is a good solution!