Examples

Click to collapse [-]

Client and server examples

This example adds a /warptoveh command which warps the player who types it to a random car.

addCommandHandler( "warptoveh",
function ( player )
local isServer = isElement( player )
local success
-- If we are on the server side, get a random car and warp the player to it
if isServer then
local veh = getRandomVehicle( )
if veh then
success = warpPedIntoVehicle( player, veh )
end
else
local veh = getRandomVehicle( )
if veh then
-- Discard clientside only vehicles we can't warp to
-- In the strange case that there are no server vehicles MTA will abort the loop after a bit
while isElementLocal( veh ) do
veh = getRandomVehicle( )
end
success = warpPedIntoVehicle( localPlayer, veh )
end
end
-- Tell the player what we did
outputChatBox( success and "You have been warped to a random vehicle." or "Could not warp you to a random vehicle.", isServer and player or ( success and 0 or 255 ), isServer and ( success and 0 or 255 ) or ( success and 255 or 0 ), isServer and ( success and 255 or 0 ) or 0, isServer and 0 or nil )
end
)

This example adds a /blowveh command to blow a random vehicle.

addCommandHandler( "blowveh",
function ( player )
local veh = getRandomVehicle( )
local success
if veh then
success = blowVehicle( veh )
end
-- Tell the player what we did
local isServer = isElement( player )
outputChatBox( success and "A random vehicle has been blown!" or "Could not blow a random vehicle.", isServer and player or ( success and 0 or 255 ), isServer and ( success and 0 or 255 ) or ( success and 255 or 0 ), isServer and ( success and 255 or 0 ) or 0, isServer and 0 or nil )
end
)

See also

Table functions

isValueInTable» This function returns true if the value exists in the table, false if the value does not exist in the table.

setTableToSql» This function is used to save the table in the database (sql).