Trying to print Letters from a Table by Random

I mentioned in my post it is bad practice to iteratively concatenate a string over and over. Lua doesn’t handle this well usually and if you want to make larger strings you’ll almost always hog a ton of memory. You end up allocating n! bytes for a string of n size.

No, this is over complicated.

Also, @ OP you may actually be looking for this sort of already built in behavior from what you explain.

Yes, because it chooses a letter or number (from 1-9) 10 times and concatenates them together to make a randomized ID.

so i need to add the Letters, A to Z and 0, 1, 2, 3, etc. to the Characters Table?

No, when the script runs, the first two loops add those in for you.

im trying to print this (obviously, to see if this works.) But its not printing by me doing print(Characters) and print(result)
Am i missing something? If this is suppose to work(how it is) then im screwing up somehow. @1waffle1

     local characters = {}
 for i = 65, 90 do
  characters[#characters+1] = string.char(i)
 end
  for i = 0, 9 do
  characters[#characters+1] = tostring(i)
end

 local function randomString()
  local result = ""
   for i = 1, 10 do
	result = result..characters[math.random(#characters)]
	
   end
  print(randomString())
  return result

end

You need to call the function.
print(randomString())

1 Like

This is alot of work for something that does not work. It Doesn’t print whats inside the table by calling the function.

When you edited your post you put print(randomString()) inside randomString. This is an infinite recursive loop. Call the function from outside of the function instead of inside of itself where it never runs because it isn’t called.

Ok When i try to set the PlrID Value by doing:

 if plrID.Value == "0" then

for i = 65, 90 do
characters[#characters+1] = string.char(i)

end

for i = 0, 9 do
characters[#characters+1] = tostring(i)
 end	
local function randomString()

local result = ""
for i = 1, 10 do
	result = result..characters[math.random(#characters)]
   end

return result

 end	
print(randomString())
plrID.Value = (randomString())
end																					

end)

it prints a random set of Numbers and Letters but gives the player a different set of numbers and letters

The function doesn’t generate the same random string every time it is called. You’re calling it twice. The first time is when you print its result and the second time is when you set the value to its result.

1 Like

Thank you. it generates the random Number and Letter pattern and prints it and add it as a ID Value for a Player.