buddem/car/carspawn.lua

47 lines
1.3 KiB
Lua

RegisterCommand('car', function(source, args)
-- Random vehicle if nothing asked
local vehicle = args[1] or vehicles[math.random(#vehicles)]
-- Check existance and if a vehicle
if not IsModelInCdimage(vehicle) or not IsModelAVehicle(vehicle) then
TriggerEvent("chat:addMessage", {
color = {255,0,0},
args = { vehicle .. ' ei ole ajoneuvo tai sitä ei löydy.' }
})
return
end
-- Load model
RequestModel(vehicle)
while not HasModelLoaded(vehicle) do
Wait(500)
end
-- Position vehicle niceishly
local ped = PlayerPedId()
local pos = GetEntityCoords(ped)
local head, vect = GetClosestVehicleNodeWithHeading(pos.x, pos.y, pos.z, 1)
local x,y,z = table.unpack(vect)
local veh = CreateVehicle(vehicle, x, y, z, GetEntityHeading(ped), true, false)
SetVehicleOnGroundProperly(veh)
-- Set register plate and a blip
SetVehicleNumberPlateText(veh, GetPlayerName())
local blip = AddBlipForEntity(veh)
SetBlipSprite(blip, 225)
SetBlipFlashes(blip, true)
SetBlipFlashTimer(blip, 10000)
-- Inform vehicle spawned
TriggerEvent('chat:addMessage', {
color = {0,255,0},
args = { 'Tässä pyytämäsi upouusi ' .. vehicle }
})
-- Cleaning up. Let R* handle despawning
SetModelAsNoLongerNeeded(vehicle)
SetEntityAsNoLongerNeeded(veh)
end, false)