CaesarNero, I hoped someone who knows what they were doing might give you some completed script. I have something working but itās embedded in a bigger script that I am playing with. When I extract the /spawn vehicle section however it doesnāt act like I expect. It seems to need a loop in the client but Iām not sure why. Anyway, you can use this below until someone does it properly ā I think it will work.
Create a folder named āSpawnā in your server āresourcesā folder and then create these files. First put the following into a file named āsv_spawn.luaā:
AddEventHandler('chatMessage', function(source, n, message)
local args = stringsplit(message, " ")
if (args[1] == "/spawn") then
CancelEvent()
if (args[2] ~= nil) then
local playerID = tonumber(source)
local vehicleName = tostring(args[2])
TriggerClientEvent('VehicleSpawn', playerID, vehicleName)
else
local event = 'chatMessage'
local eventTarget = source
local messageSender = "SYSTEM"
local messageSenderColor = {200, 0, 0}
local message = "Usage: /spawn <Vehicle_Name> (for example /spawn polmav)"
TriggerClientEvent(event, eventTarget, messageSender, messageSenderColor, message)
end
end
end)
function stringsplit(self, delimiter)
local a = self:Split(delimiter)
local t = {}
for i = 0, #a - 1 do
table.insert(t, a[i])
end
return t
end
Second put the following in a file named ācl_spawn.luaā:
RegisterNetEvent('VehicleSpawn')
AddEventHandler('VehicleSpawn', function(vehicleName)
local myPed = GetPlayerPed(-1)
local player = PlayerId()
local vehicle = GetHashKey(vehicleName)
RequestModel(vehicle)
while not HasModelLoaded(vehicle) do
Wait(1)
end
local coords = GetOffsetFromEntityInWorldCoords(GetPlayerPed(-1), 0, 5.0, 0)
local spawned_car = CreateVehicle(vehicle, coords, GetEntityHeading(myPed), true, false)
SetVehicleOnGroundProperly(spawned_car)
SetModelAsNoLongerNeeded(vehicle)
Citizen.InvokeNative(0xB736A491E64A32CF,Citizen.PointerValueIntInitialized(spawned_car))
end)
Citizen.CreateThread(function()
while true do
Wait(1)
end
end)
Third create your ā__resource.luaā file in the same folder with the following:
client_script "cl_spawn.lua"
server_script "sv_spawn.lua"
And finally, in the citmp-server.yml file in your main server folder, add ā- spawnā to the end of your list of āAutoStartResources:ā.