# [SOLVED] Generate random license plate text on vehicle creation

## Recommended Posts

Hey all, so I'm struggling trying to understand how I could create a random license plate when a vehicle spawns.

I got it down to generate random numbers, but I was hoping to have the license plate be 3 characters followed by 3 numbers. How would I go about getting this done?

Edited by Stealthy Serval

Does it matter to be unique?
Can two different vehicles have the same license plate?

If don't matter:

```local characters = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}

function createRandomPlateText()
local plate = ""

for i = 1, 3 do
plate = plate..characters[math.random(1, #characters)]
end
plate = plate.."-"
for i = 1, 3 do
plate = plate..math.random(1, 9)
end

return plate
end```

Edited by Patrick2562
• 1

9 minutes ago, Patrick2562 said:

Does it matter to be unique?
Can two different vehicles have the same license plate?

If don't matter:

```
local characters = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}

function createRandomPlateText()
local plate = ""

for i = 1, 3 do
plate = plate..characters[math.random(1, #characters)]
end
plate = plate.."-"
for i = 1, 3 do
plate = plate..math.random(1, 9)
end

return plate
end
```

It does need to be unique, but I actually have a check in place for that!

That works perfectly, thank you so much. I clearly need to learn more Lua because I really don't understand the whole "for do" statements.

Edited by Stealthy Serval

Could make it so it's 100% unique by using tables. If there's a conflict simply rerun the function.

## Create an account

Register a new account