I tried this and it doesn’t seem to change anything sadly. By the way I suppose that the control over the entity already works because I’m the last person to get out of the car, as well as because I can open the doors, trunk, etc… from outside
After some testing, I found out that the radio can’t be enabled when a ped isn’t in the vehicle. A workaround which turned out to suffice was to spawn a ped inside the vehicle, then turn the radio on, and finally delete the entity. I am not sure if there’s a better way of doing it, but this definitely works:
RegisterCommand(
'radio',
function()
local nearestVehicle = GetNearestVehicle()
if nearestVehicle then
EnableVehicleRadio(nearestVehicle, "RADIO_02_POP") -- Change the radio station to whichever you want to (https://pastebin.com/Kj9t38KF)
end
end
)
function RequestControlOfEntity(entity, limit)
limit = limit or 20
local counter = 0
if not NetworkHasControlOfEntity(entity) then
NetworkRequestControlOfEntity(entity)
Citizen.Wait(50)
while not NetworkHasControlOfEntity(entity) and counter >= limit do
counter = counter + 1
Citizen.Wait(50)
end
end
end
function GetNearestVehicle() -- Gets any nearest vehicle in the radius of 1.5 units
local playerPed = PlayerPedId()
local playerCoords = GetEntityCoords(playerPed)
local pointB = GetEntityForwardVector(playerPed) * 0.001 + playerCoords
local shapeTest = StartShapeTestCapsule(playerCoords.x, playerCoords.y, playerCoords.z, pointB.x, pointB.y, pointB.z, 1.5, 10, playerPed, 7)
local _, hit, _, _, entity = GetShapeTestResult(shapeTest)
return (hit == 1 and IsEntityAVehicle(entity)) and entity or false
end
function EnableVehicleRadio(vehicle, radioStation)
if vehicle then
local model = GetHashKey("a_m_y_acult_01")
RequestModel(model) -- Load the ped to be spawned in the vehicle
while not HasModelLoaded(model) do
Citizen.Wait(50)
end
local ped = CreatePedInsideVehicle(vehicle, 0, model, -1, true, true) -- Spawn the ped
SetEntityAlpha(ped, 0.0) -- Make the ped invisible
SetModelAsNoLongerNeeded(model) -- Release the model from memory
RequestControlOfEntity(vehicle) -- Request control over the vehicle (to be able to turn on the engine)
SetVehicleEngineOn(vehicle, true, false, false) -- Start the engine and wait until it's started
while not GetIsVehicleEngineRunning(vehicle) do
Citizen.Wait(10)
end
SetVehRadioStation(vehicle, radioStation) -- Set the radio station
SetVehicleRadioLoud(vehicle, true)
SetVehicleRadioEnabled(vehicle, true) -- Turn the radio on
DeleteEntity(ped) -- Remove the ped, we don't need it anymore
end
end
Keep in mind that the radio also doesn’t seem to enable when the engine is shut off, so you have to start it before enabling the radio. Killing the engine also turns the radio off.