Jump to content

setVehiclePlateText partially works


Recommended Posts

I currently have a command to spawn a vehicle using either the model ID or the vehicle name, and when you spawn the vehicle, it assigns a random plate to the vehicle.


This works perfectly if you use the vehicle model ID, but if you use the name (ie "Tampa") it returns an error:

WANRING: vehicle-system\vs_server.lua:184: Bad argument @ 'setVehiclePlateText' [Expected string at argument 2, got nil]

I've moved my variables around, called the function in and out of the different sections of code, but still don't understand why it's doing this.
Here is my command:

--Create Vehicle/Spawn Vehicle from ID or name
function commandCreateVehicle(thePlayer,command,vehicleModel)
    if(vehicleModel)then
        local vehicleToSpawn = tonumber(vehicleModel)
        local vehicleOwner = getPlayerName(thePlayer)
        local vehTag = createRandomPlateText()
        local x,y,z = getElementPosition(thePlayer)
        x = x+5
        z = z+2
        if(vehicleToSpawn ~= nil)then
            if(vehicleToSpawn > 399)then
                if(vehicleToSpawn < 612)then
                    local theVehicle = createVehicle(vehicleToSpawn,x,y,z)
                    if(theVehicle)then
                        local vehicleName = getVehicleNameFromModel(vehicleToSpawn)
                        vehicleTag = vehicleTag + 1
                        setElementData(theVehicle,"name",vehicleName)
                        setElementData(theVehicle,"model",vehicleToSpawn)
                        setElementData(theVehicle,"tag",vehTag)
                        setElementData(theVehicle,"id",vehicleTag)
                        setVehicleOverrideLights(theVehicle,1)
                        setVehiclePlateText(theVehicle,vehTag)
                        setElementData(theVehicle,"owner",vehicleOwner)
                        outputChatBox("(#"..vehicleTag..") Enjoy your "..vehicleName..", "..getPlayerName(thePlayer).."!",thePlayer,0,255,0)
                    else
                        outputChatBox("Vehicle failed to spawn. Error: SM01.",thePlayer,255,0,0)
                    end
                else
                    outputChatBox("Invalid vehicle ID.",thePlayer,255,0,0)
                end
            else
                outputChatBox("Invalid vehicle ID.",thePlayer,255,0,0)
            end
        else
            local theVehicle = createVehicle(getVehicleModelFromName(vehicleModel),x,y,z)
            if(theVehicle)then
                local vehicleID = getVehicleModelFromName(vehicleModel)
                local vehicleName = getVehicleNameFromModel(vehicleID)
                vehicleTag = vehicleTag + 1
                setElementData(theVehicle,"name",vehicleName)
                setElementData(theVehicle,"model",vehicleID)
                setElementData(theVehicle,"tag",vehTag)
                setElementData(theVehicle,"id",vehicleTag)
                setVehiclePlateText(theVehicle,vehTag)
                setElementData(theVehicle,"owner",vehicleOwner)
                setVehicleOverrideLights(theVehicle,1)
                outputChatBox("(#"..vehicleTag..") Enjoy your "..vehicleName..", "..getPlayerName(thePlayer).."!",thePlayer,0,255,0)
            else
                outputChatBox("Invalid vehicle name.",thePlayer,255,0,0)
            end
        end
    else
        outputChatBox("Invalid syntax. Please use: /createvehicle [vehicle ID/name]",thePlayer,255,0,0)
    end
end
addCommandHandler("createvehicle",commandCreateVehicle)
addCommandHandler("cv",commandCreateVehicle)

and here is the createRandomPlateText() function
 

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
    qh = sql:query("SELECT * FROM vehicles WHERE tag=?",plate)
    local row = qh[1]
    if row then
        createRandomPlateText()
    else
    	return plate
    end
end

 

Link to comment

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...