Random name via script?

How can I make it so every part that I clone gets a random name?

For example, I have a script that clones a part and gives the part a random name like A263, or R712. How can I do that in a script?

example script

partClone = part:Clone()
partClone.Name = "random stuff"

You make a table, which has all the letters which are allowed in this random name. (e.g. Alphabet and Numbers)
You then use math.random() to generate random numbers which you will use to pick random letters/numbers/symbols from the table.

local alphabet = {}
local characters = 12 -- The amount of characters this name should have

local function randomString()
local String = ""
local num = 0
repeat
num += 1
String = String..alphabet[math.random(1, #alphabet)]
until num == characters
return String
end

print(randomString())
1 Like

You can use HttpService to generate a UUID (universally unique identifier) that is guaranteed to be unique.

local HttpService = game:GetService("HttpService")


local result = HttpService:GenerateGUID(true)


print(result) --> Example output: {04AEBFEA-87FC-480F-A98B-E5E221007A90}
2 Likes

Anyway to make the output shorter? (if the example is that long)

You can use string:sub() to do this

1 Like
local random = math.random
local char = string.char
local function randomstring(length)
length = length or random(8, 16)
local str = ""
for ind = 1, length do
if random(1,2) == 1 then
str = str .. char(random(65, 90))
else
str = str .. char(random(97, 122))
end
end
return str
end

for i_edited_this_like_four_times = 1, 10 do
Instance.new("Hint", workspace).Name = randomstring(12)
end
1 Like

All the solutions provided are great, but if you want a quick method Iā€™d just do tostring({}):sub(10)

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.