Hi i made a script from the cfx guide creating you first script. how to spawnPlayer and how to spawn a car from the chat with /car command this works fine, but i want the car to spawn automatically when the players spawns. So i dont have to use the chat /car command.
But how do i run that command /car inside the script?
You can create a function and insert the content of RegisterCommand(ācarā, function() in it. Then inside the chat event, you require the function you created. Like:
function spawnVehicle(vehicleName)
-- Spawn vehicle script
end
RegisterCommand("car", function(source, args)
spawnVehicle(args[1]) -- This will execute the function spawnVehicle when you use the chat command
end)
spawnVehicle("vehicleName") -- And you can put this at any point of the script and it will execute the spawnVehicle function
Just change āvehicleNameā with the name of the vehicle you want to spawn.
Thanks for your reply but, i did not write it clear enough. The car command /car works for me.
but i want that /car command to be run automatically when the players spawns. so i dont have to go to chat and write /car.
Ok so this should work fine, I havenāt tested it.
local defaultVehicle = "adder"
function spawnVehicle(vehicleName)
-- Check if player insert a vehicle name, if not it will spawn a default car
vehicleName = vehicleName or defaultVehicle
-- check if the vehicle actually exists
if not IsModelInCdimage(vehicleName) or not IsModelAVehicle(vehicleName) then
TriggerEvent('chat:addMessage', {
args = { 'It might have been a good thing that you tried to spawn a ' .. vehicleName .. '. Who even wants their spawning to actually ^*succeed?' }
})
return
end
-- load the model
RequestModel(vehicleName)
-- wait for the model to load
while not HasModelLoaded(vehicleName) do
Wait(500) -- often you'll also see Citizen.Wait
end
-- get the player's position
local playerPed = PlayerPedId() -- get the local player ped
local pos = GetEntityCoords(playerPed) -- get the position of the local player ped
-- create the vehicle
local vehicle = CreateVehicle(vehicleName, pos.x, pos.y, pos.z, GetEntityHeading(playerPed), true, false)
-- set the player ped into the vehicle's driver seat
SetPedIntoVehicle(playerPed, vehicle, -1)
-- give the vehicle back to the game (this'll make the game decide when to despawn the vehicle)
SetEntityAsNoLongerNeeded(vehicle)
-- release the model
SetModelAsNoLongerNeeded(vehicleName)
-- tell the player
TriggerEvent('chat:addMessage', {
args = { 'Woohoo! Enjoy your new ^*' .. vehicleName .. '!' }
})
end
RegisterCommand('car', function(source, args) -- Register chat command
spawnVehicle(args[1]) -- Trigger spawnVehicle function
end, false)
AddEventHandler("playerSpawned", function() -- Fire when player spawns
spawnVehicle(defaultVehicle) -- Trigger spawnVehicle function
end)
All I did was instead of putting the script inside the RegisterCommand event I created a function that will fire when the command is written or when the player spawns. You can change the default vehicle at the top.
Wow thanks it worked for me, so nice. And if i dont want to spawn inside car value is = 1. then player would spawn in the roof of the car LOL. So i guess i could use something like this to spawn beside the car.
ā create the vehicle
local vehicle = CreateVehicle(vehicleName, pos.141.95, pos.1305.06, pos.29.17, GetEntityHeading(playerPed), false, true)
-- set the player ped into the vehicle's driver seat
SetPedIntoVehicle(playerPed, vehicle, 1)
or is this total nonsens my code
Nope i canāt do that just tested it
AHHaaa i got to work the noob i am ā create the vehicle beside the player.
local vehicle = CreateVehicle(vehicleName, 139.19, -1305.24, 28.75, GetEntityHeading(playerPed), true, false)