Phoenix-Roleplay Posted July 14, 2012 Posted July 14, 2012 Hello MTA, I have been trying to create a fully functional vehicle spawn system but it seems at line 52 I get the following error: ERROR: pf\s_main.lua:52: attempt to compare string with number I read somewhere that lua will convert numbers to strings and likewise. Anyways, here is the script function createVehicleCommand ( thePlayer, commandName, carName ) local x, y, z = getElementPosition ( thePlayer ) if tonumber(carName) ~= nil then if carName < 400 then outputChatBox("Invalid Vehicle", player, 255, 0, 0) elseif carName > 611 then outputChatBox("Invalid Vehicle", player, 255, 0, 0) else local carM = getVehicleName(carName) createVehicle(carName, x+5, y, z) outputChatBox("Your " .. getVehicleName(carM) .. " (#" .. carM .. ") was spawned with an ID of " .. vehicles.id, player, 0, 255, 0) vehicles.id = vehicles.id+1 end else local carModel = getVehicleModelFromName(carName) if not carModel then outputChatBox("That is not a valid car name") else createVehicle(carModel, x+5, y, z) outputChatBox("Your " .. getVehicleName(carModel) .. " (#" .. carModel .. ") was spawned with and ID of " .. vehicles.id, player, 0, 255, 0) vehicles.id = vehicles.id+1 end end end addCommandHandler("v", createVehicleCommand) Line 52: if carName < 400 then Sorry if the script is slightly poorly put together, I was in a rush when trying to make this part of it.
Castillo Posted July 14, 2012 Posted July 14, 2012 Command arguments will be always a string, you have to convert it to a number.
Phoenix-Roleplay Posted July 14, 2012 Author Posted July 14, 2012 Right. How would I approach whilst keeping it able to detect if someone puts in a vehicle name? Sorry, I am not familiar with working with vehicles.
Castillo Posted July 14, 2012 Posted July 14, 2012 local carName = tonumber ( carName ) if ( type ( carName ) == "number" ) then if ( carName < 400 or carName > 611 ) then outputChatBox ( "Invalid Vehicle", player, 255, 0, 0 ) else -- Your code end end
Phoenix-Roleplay Posted July 14, 2012 Author Posted July 14, 2012 It appears that it jumps straight to "That is not a valid car name"
Phoenix-Roleplay Posted July 14, 2012 Author Posted July 14, 2012 function createVehicleCommand ( thePlayer, commandName, carName ) local x, y, z = getElementPosition ( thePlayer ) if (type(carName) == "number") then if carName < 400 then outputChatBox("Invalid Vehicle", player, 255, 0, 0) elseif carName > 611 then outputChatBox("Invalid Vehicle", player, 255, 0, 0) else local carM = getVehicleName(carName) createVehicle(carName, x+5, y, z) outputChatBox("Your " .. getVehicleName(carM) .. " (#" .. carM .. ") was spawned with an ID of " .. vehicles.id, player, 0, 255, 0) vehicles.id = vehicles.id+1 end else local carModel = getVehicleModelFromName(carName) if not carModel then outputChatBox("That is not a valid car name") else createVehicle(carModel, x+5, y, z) outputChatBox("Your " .. getVehicleName(carModel) .. " (#" .. carModel .. ") was spawned with and ID of " .. vehicles.id, player, 0, 255, 0) vehicles.id = vehicles.id+1 end end end addCommandHandler("v", createVehicleCommand)
Castillo Posted July 14, 2012 Posted July 14, 2012 function createVehicleCommand ( thePlayer, commandName, carName ) local x, y, z = getElementPosition ( thePlayer ) local carModel = tonumber ( carName ) if ( type ( carModel ) == "number" ) then if ( carModel < 400 or carModel > 611 ) then outputChatBox ( "Invalid Vehicle", player, 255, 0, 0 ) else createVehicle ( carModel, x + 5, y, z ) outputChatBox ( "Your " .. getVehicleName ( carModel ) .. " (#" .. carModel .. ") was spawned with an ID of " .. vehicles.id, player, 0, 255, 0 ) vehicles.id = ( vehicles.id + 1 ) end else local carModel = getVehicleModelFromName ( carName ) if ( not carModel ) then outputChatBox ( "That is not a valid car name", player, 255, 0, 0 ) else createVehicle ( carModel, x + 5, y, z ) outputChatBox ( "Your " .. getVehicleName ( carModel ) .. " (#" .. carModel .. ") was spawned with and ID of " .. vehicles.id, player, 0, 255, 0 ) vehicles.id = ( vehicles.id + 1 ) end end end addCommandHandler ( "v", createVehicleCommand )
Cadu12 Posted July 14, 2012 Posted July 14, 2012 (edited) function createVehicleCommand ( thePlayer, commandName, carName ) local x, y, z = getElementPosition ( thePlayer ) if (type(tonumber(carName)) == "number") then if tonumber(carName) > 400 and tonumber(carName) < 611 then outputChatBox("Invalid Vehicle", player, 255, 0, 0) else local id = getElementModel(carName) createVehicle(carName, x+5, y, z) outputChatBox("Your " .. getVehicleName(id) .. " (#" .. tonumber(carName) .. ") was spawned with an ID of " .. vehicles.id, player, 0, 255, 0) vehicles.id = vehicles.id+1 end else local carModel = getVehicleModelFromName(carName) if not carModel then outputChatBox("That is not a valid car name") else createVehicle(carModel, x+5, y, z) outputChatBox("Your " .. getVehicleName(carModel) .. " (#" .. carModel .. ") was spawned with and ID of " .. vehicles.id, player, 0, 255, 0) vehicles.id = vehicles.id+1 end end end addCommandHandler("v", createVehicleCommand) Edited July 14, 2012 by Guest
Jaysds1 Posted July 14, 2012 Posted July 14, 2012 function createVehicleCommand ( thePlayer, commandName, carName ) local x, y, z = getElementPosition ( thePlayer ) if (type(carName) == "number") then if tonumber(carName) > 400 and tonumber(carName) < 611 then outputChatBox("Invalid Vehicle", player, 255, 0, 0) else local carM = getVehicleName(carName) createVehicle(carName, x+5, y, z) outputChatBox("Your " .. getVehicleName(carM) .. " (#" .. carM .. ") was spawned with an ID of " .. vehicles.id, player, 0, 255, 0) vehicles.id = vehicles.id+1 end else local carModel = getVehicleModelFromName(carName) if not carModel then outputChatBox("That is not a valid car name") else createVehicle(carModel, x+5, y, z) outputChatBox("Your " .. getVehicleName(carModel) .. " (#" .. carModel .. ") was spawned with and ID of " .. vehicles.id, player, 0, 255, 0) vehicles.id = vehicles.id+1 end end end addCommandHandler("v", createVehicleCommand) There is no use of checking if "carName" type is a number, because it's going to come as a string.
Jaysds1 Posted July 14, 2012 Posted July 14, 2012 oh, sorry, I've never noticed. Anyways, Phoenix, Did it worked?
Phoenix-Roleplay Posted July 14, 2012 Author Posted July 14, 2012 Checking now! Sorry, was busy with halo reach :3
Phoenix-Roleplay Posted July 14, 2012 Author Posted July 14, 2012 Oh don't judge! I'm sorta a fan boy of Halo, grew up off that game lol Anyways, @Cadu, it does spawn the vehicle using either a number or name. But on both parts I get: WARNING: pf\s_main.lua:56: Bad argument @ `getVehicleName` WARNING: pf\s_main.lua:58: Bad argument @ `getVehicleName` ERROR: pf\s_main.lua:58: attempt to concatenate local `carM` (a boolean value)
Castillo Posted July 14, 2012 Posted July 14, 2012 Instead of keeping just with one answer, check mine as well, it should work.
Phoenix-Roleplay Posted July 14, 2012 Author Posted July 14, 2012 I actually jumped over yours by mistake checking it now.
Jaysds1 Posted July 14, 2012 Posted July 14, 2012 Try my code, I edited it. , calm down, Everyone, He's only one person
Phoenix-Roleplay Posted July 14, 2012 Author Posted July 14, 2012 @Castillo, yours works but fails to display the line where it says "Your vehicle was spawned.." @Cadu, same problems. EDIT: @jays, yes but I work like a million
Jaysds1 Posted July 14, 2012 Posted July 14, 2012 try this: function createVehicleCommand ( thePlayer, commandName, carName ) local x, y, z = getElementPosition ( thePlayer ) if (type(tonumber(carName)) == "number") then if tonumber(carName) > 400 and tonumber(carName) < 611 then outputChatBox("Invalid Vehicle", player, 255, 0, 0) else local carM = getVehicleNameFromModel(carName) createVehicle(carName, x+5, y, z) outputChatBox("Your " .. carM .. " (#" .. carName .. ") was spawned with an ID of " .. vehicles.id, player, 0, 255, 0) vehicles.id = vehicles.id+1 end else local carModel = getVehicleModelFromName(carName) if not carModel then outputChatBox("That is not a valid car name") else createVehicle(carModel, x+5, y, z) outputChatBox("Your " .. getVehicleName(carModel) .. " (#" .. carModel .. ") was spawned with and ID of " .. vehicles.id, player, 0, 255, 0) vehicles.id = vehicles.id+1 end end end addCommandHandler("v", createVehicleCommand)
Phoenix-Roleplay Posted July 14, 2012 Author Posted July 14, 2012 @Jays, it seems using yours no matter what I get the "Your (#...) was spawned.."
Castillo Posted July 14, 2012 Posted July 14, 2012 function createVehicleCommand ( thePlayer, commandName, carName ) local x, y, z = getElementPosition ( thePlayer ) local carModel = tonumber ( carName ) if ( type ( carModel ) == "number" ) then if ( carModel < 400 or carModel > 611 ) then outputChatBox ( "Invalid Vehicle", player, 255, 0, 0 ) else createVehicle ( carModel, x + 5, y, z ) outputChatBox ( "Your " .. getVehicleNameFromModel ( carModel ) .. " (#" .. carModel .. ") was spawned with an ID of " .. vehicles.id, player, 0, 255, 0 ) vehicles.id = ( vehicles.id + 1 ) end else local carModel = getVehicleModelFromName ( carName ) if ( not carModel ) then outputChatBox ( "That is not a valid car name", player, 255, 0, 0 ) else createVehicle ( carModel, x + 5, y, z ) outputChatBox ( "Your " .. getVehicleNameFromModel ( carModel ) .. " (#" .. carModel .. ") was spawned with and ID of " .. vehicles.id, player, 0, 255, 0 ) vehicles.id = ( vehicles.id + 1 ) end end end addCommandHandler ( "v", createVehicleCommand )
Jaysds1 Posted July 14, 2012 Posted July 14, 2012 (edited) ok, I tried something else, here: function createVehicleCommand ( thePlayer, _, carName ) local x, y, z = getElementPosition ( thePlayer ) local carModel = getVehicleModelFromName(carName) local carName = getVehicleNameFromModel(tonumber(carName)) if carModel then createVehicle(carModel, x+5, y, z) outputChatBox("Your " .. getVehicleNameFromModel(carModel) .. " (#" .. carModel .. ") was spawned with an ID of " .. vehicles.id, thePlayer, 0, 255, 0) elseif carName then local carM = getVehicleModelFromName(carName) createVehicle(carM, x+5, y, z) outputChatBox("Your " .. carName .. " (#" .. carM .. ") was spawned with and ID of " .. vehicles.id, thePlayer, 0, 255, 0) else outputChatBox("That is not a valid car name/model",thePlayer) end end addCommandHandler("v", createVehicleCommand) Edited July 14, 2012 by Guest
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now