How do i run /car command automatically from inside a script. without using chat the command

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?

Link to guide https://docs.fivem.net/docs/scripting-manual/introduction/creating-your-first-script/

Kind regards

MAXITHX

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.

Hope this helps.

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.

Well in that case you can use playerSpawned:

AddEventHandler("playerSpawned", function()
    -- Stuff
end)

that will fire every time the player spawns

I have to read up on that and figure it out, i know so little as to nothing about lua script or programming LOL.

could you maybe make the script for me, that runs that chat command /car for me. :slight_smile:
so i maybe can see what going on.

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.

1 Like

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 :slight_smile:

Nope i can’t do that just tested it

AHHaaa i got to work the noob i am :slight_smile: – create the vehicle beside the player.

local vehicle = CreateVehicle(vehicleName, 139.19, -1305.24, 28.75, GetEntityHeading(playerPed), true, false)

1 Like