Jump to content

Temporary admin vehicle command


Dzsozi (h03)

Recommended Posts

Hello! I'm making my own admin system for my gamemode and I have some issues.

I would like to make temporary vehicles that admins can create, and I would like to make a command for that, so admins can also delete the specified vehicle. Only problem with the delete command is that if I create for example 2 vehicles, so there are a vehicle with dbid -1 and dbid -2, I can't delete the vehicle with dbid -1, only if I delete dbid -2, so I can always delete only the last vehicle created, not the vehicle I would like to delete. Why is this happening? The commands:

totalTempVehicles = 0

function createTempVehicle(thePlayer, commandName, ...)
	if (exports.vice_core:isHarmasAdmin(thePlayer)) then
		local args = {...}
		if (#args < 1) then
			outputChatBox("[USAGE]:#FFFFFF /"..commandName.." [vehicle] [color 1] [color 2]", thePlayer, 0, 180, 255,true)
		else
			local vehicleID = tonumber(args[1])
			
			local col1 = #args ~= 1 and tonumber(args[#args - 1]) or -1
			local col2 = #args ~= 1 and tonumber(args[#args]) or -1
			
			if not vehicleID then -- vehicle is specified as name
				local vehicleEnd = #args
				repeat
					vehicleID = getVehicleModelFromName(table.concat(args, " ", 1, vehicleEnd))
					vehicleEnd = vehicleEnd - 1
				until vehicleID or vehicleEnd == -1
				if vehicleEnd == -1 then
					exports.vice_notification:addNotification(thePlayer, "There is no vehicle with this name", "error")
					return
				elseif vehicleEnd == #args - 2 then
					col2 = math.random(0,100)
				elseif vehicleEnd == #args - 1 then
					col1 = math.random(0,100)
					col2 = math.random(0,100)
				end
			end
			
			local r = getPedRotation(thePlayer)
			local x, y, z = getElementPosition(thePlayer)
			x = x + ( ( math.cos ( math.rad ( r ) ) ) * 5 )
			y = y + ( ( math.sin ( math.rad ( r ) ) ) * 5 )
			
			local letter1 = string.char(math.random(65,90))
			local letter2 = string.char(math.random(65,90))
			local plate = letter1 .. letter2 .. math.random(0, 9) .. " " .. math.random(1000, 9999)
			
			if vehicleID then
				local veh = createVehicle(vehicleID, x, y, z, 0, 0, r, plate)
				
				if not (veh) then
					exports.vice_notification:addNotification(thePlayer, "There is no vehicle with this ID", "error")
				else
					setVehicleDamageProof(veh, false)

					totalTempVehicles = totalTempVehicles + 1
					local dbid = (-totalTempVehicles)
					
					setVehicleColor(veh, col1, col2, col1, col2)
					
					setElementInterior(veh, getElementInterior(thePlayer))
					setElementDimension(veh, getElementDimension(thePlayer))
					
					setVehicleOverrideLights(veh, 1)
					setVehicleEngineState(veh, false)
					setVehicleFuelTankExplodable(veh, false)
					
					setElementData(veh, "dbid", dbid)
					exports.vice_chat:sendMessageToAdmins("#00FFB4" .. getPlayerName(thePlayer) .. "#FFFFFF Created a temporary vehicle. #00FFB4(Vehicle: " .. getVehicleName(veh) .. " - ID: " .. dbid .. ")")
				end
			else
				exports.vice_notification:addNotification(thePlayer, "There is no vehicle with this ID", "error")
			end
		end
	end
end
addCommandHandler("veh", createTempVehicle, false, false)

----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------

function deleteVehicle(thePlayer, commandName, id)
	if (exports.vice_core:isSysAdmin(thePlayer)) then
		local dbid = tonumber(id)
		if not (dbid) then
			outputChatBox("[USAGE]:#FFFFFF /"..commandName.." [vehicle ID]", thePlayer, 0, 180, 255,true)
		else
			for _, v in ipairs(getElementsByType("vehicle")) do
				vehicleDBID = getElementData(v, "dbid")
				theVehicle = v
			end
			if (vehicleDBID == dbid) then
				if (vehicleDBID<0) then
					destroyElement(theVehicle)
				end
				exports.vice_notification:addNotification(thePlayer, "Vehicle successfully deleted", "success")
			else
				exports.vice_notification:addNotification(thePlayer, "There is no existing vehicle with this ID", "error")
			end
		end
	end
end
addCommandHandler("delveh", deleteVehicle, false, false)
Link to comment

Okay, here's what I came up with:


function deleteVehicle(thePlayer, commandName, id)
	if (exports.vice_core:isSysAdmin(thePlayer)) then
	local dbid = tonumber(id)
		if tonumber(dbid) then
			for _, v in ipairs(getElementsByType("vehicle")) do
			local vehicleDBID = getElementData(v, "dbid")
				if tonumber(vehicleDBID) == tonumber(dbid) then
					destroyElement(v)
					exports.vice_notification:addNotification(thePlayer, "Vehicle successfully deleted", "success")
				else
					exports.vice_notification:addNotification(thePlayer, "There is no existing vehicle with this ID", "error")
				end
			end	
		else
			outputChatBox("[USAGE]:#FFFFFF /"..commandName.." [vehicle ID]", thePlayer, 0, 180, 255,true)
		end
	end
end	
addCommandHandler("delveh", deleteVehicle, false, false)

 

Link to comment

It works, but for some reason if there are 2 vehicles (id -1 and id -2) and if I delete id -1 I get the error message, so it says that there is no existing vehicle, but the vehicle itself gets deleted, but if I delete -2, so the last vehicle created, I get the success message (the vehicle gets deleted as well, so no problem).

Link to comment

Got it fixed, thank you! Here's how if anybody having a problem like this or if you are interested:

function deleteVehicle(thePlayer, commandName, id)
	if (exports.vice_core:isSysAdmin(thePlayer)) then
	local dbid = tonumber(id)
	local destroyedVehicle = false
	local vehicleName = nil
		if tonumber(dbid) then
			for _, v in ipairs(getElementsByType("vehicle")) do
				vehicleDBID = getElementData(v, "dbid")
				if tonumber(vehicleDBID) == tonumber(dbid) then
					vehicleName = getVehicleName(v)
					destroyElement(v)
					destroyedVehicle = true
					break
				end
			end
			if destroyedVehicle == true then
				exports.vice_notification:addNotification(thePlayer, "Vehicle successfully deleted", "success")
				exports.vice_chat:sendMessageToAdmins("#00B4FF" .. getPlayerName(thePlayer) .. "#FFFFFF has deleted a vehicle. #00B4FF(Vehicle: " .. vehicleName .. " - ID: " .. dbid .. ")")
			else
				exports.vice_notification:addNotification(thePlayer, "No vehicle existing with this ID", "error")
			end
		else
			outputChatBox("[USAGE]:#FFFFFF /"..commandName.." [vehicle ID]", thePlayer, 0, 180, 255,true)
		end
	end
end	
addCommandHandler("delveh", deleteVehicle, false, false)

 

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...