How can I make a gui that gives you a random number, then puts it on a surface gui?

  1. What do you want to achieve?
    I would like to achieve a number generator, that generates a number between 001 → 999 with no decimal, and gives every player a unique number. It then puts it on a surface gui (attached image.)
    image

  2. What is the issue?
    Nobody has known how to do it, and I cannot figure it out myself either.

  3. What solutions have you tried so far?
    I have looked on the developer hub, and tried to find a post like mine however I have not found any posts like mine and none of my friends know how.

    Thanks in advance.

local randomID = math.random(0,999)

This does not work, any ideas?

Hold on im making a script right now that may work

Well, this depends on if you want it to generate every x seconds or just once when the server is starting.

Use the randomid and set the surface gui’s text to the randomid variable and it should work.

Yeah but it isnt that simple he wants every number to be unique I mean 0 to 999 is a huge range, the probability of like a 15-20 player game that 2 people get the same ID is highely unlikely but still could happen

Oh.

Put this into starterplayerscripts:

local Player = game.Players.LocalPlayer 
local gui = workspace.SurfaceGui.TextLabel - Reference your textlabel

local randnum = math.Random(0, 999)

gui.Text = randnum

print(“randnum”)

Let me know if it works :slight_smile:

Alright I wrote up this quick recursion which checks if other players have gotten the same ID and then regenerates it and etc.

It’s pretty simple to convert this to a gui that has a number on it

local Identifiers = {}
local Players = game.Players

game.Players.PlayerAdded:Connect(function(player)
	local randomIdentifier = math.random(0,999)
	local function checkOtherIdentifiers(id)
		for _, others in ipairs(Players:GetChildren()) do
			if table.getn(Identifiers) > 0 and id == Identifiers[others.UserId].ID then
				id = math.random(0,999)
				checkOtherIdentifiers(id)
			else
				Identifiers[player.UserId] = {
					ID = id
				}
			end
		end
	end
	checkOtherIdentifiers(randomIdentifier)
	print(Identifiers[player.UserId].ID)
end)

image
image

If a player leaves the game, then the entry must be removed from the table, otherwise the table becomes larger and larger. Or am I mistaken?

1 Like

You are correct, you will need a player removing event inside of the function and remove the number from the table.

Yeah your correct but i didnt add that because well, I thought OP would be able to put the pieces together themselves

Were not suppose to write whole systems but we can use code to lead someone in the right direction
@Dev_OnCake
@Carl_Weezer13

ahh, thank you all for helping me out a bit