Sign in to follow this  
Stealthy Serval

[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

Share this post


Link to post

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
  • Thanks 1

Share this post


Link to post
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

Share this post


Link to post

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

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.