[Help] Looking to Network a Vehicle

Good day,

I am working on a script that will spawn 1 vehicle for the entire server… (for example lets say: 1 single buss that could drive around to different buss stops and pick up passengers - or whatever.)

I have the Vehicle spawning on the client side an thus, each time a new client joins a new vehicle spawns… This is not what i want… In addition, i am hoping to add a blip to the map for said vehicle but it all appears to be broken as i can not for the life of me figure out how to network the vehicle (aka prevent more than 1 total vehicle from spawning and add a blip to said vehicle).

Any help or advice would be greatly appreciated!
Thanks for your time,
Jay

You can check when the first player joins on the server and spawn the vehicle on that one client. After that you can check if the server is empty and rinse repeat.

I haven’t worked much with networking and spawning yet. If that one player that “spawned” the bus left the server (but other people were still on the server), would that vehicle still remain and be functional?

Alright, 1 week, NO LUCK… the only thing i got was to get the script to spawn in only 1 vehicle by using

if NetworkIsHost() then

However, the host loses control of the veh once they leave the area and another client gets close to the veh… thus, the script thinks the entity no longer exists which is resulting in me having issues with setting the blip! :frowning:
im using:

veh = CreateVehicle(vehmodel, posx, posy, posz, vehheading, true, true)
VehToNet(veh)
N_0x06faacd625d80caa(veh)

Im then sending this to the server

TriggerServerEvent("sendtoserver:vehnetid", NetworkGetNetworkIdFromEntity(veh), true)

This is what my server.lua looks like:

RegisterServerEvent("sendtoserver:vehnetid")
AddEventHandler("sendtoserver:vehnetid", function(vehid, enable)
	if enable then
		TriggerClientEvent("sendtoclient:vehnetid", -1, vehid, true)	
	end
end)

and last but not least… my client:

local vehblip = {}
RegisterNetEvent("sendtoclient:vehnetid")
AddEventHandler("sendtoclient:vehnetid", function(vehid, enable)
    if enable then
    Citizen.Trace(vehid)
        vehblip[vehid] = AddBlipForEntity(NetworkGetEntityFromNetworkId(vehid))
    end
end)

While i am able to send the NetID from the host to the server, then back to any connected client… None of the clients recognize the vehicle exists… *i double checked by using:

if NetworkDoesEntityExistWithNetworkId(NetworkGetNetworkIdFromEntity(veh)) then
    Citizen.Trace("entity exists")
end

*I also tried the above method while sending the entity veh to the server and not the id, *same issues… (other clients wont recognize the veh exists)

**Also, (even if i had each client have their own vehicle spawn in… or not…) if another client gets near the veh. The closest client apparently takes control over the veh and then the host will re-spawn the vehicle as the script/host(client) thinks the veh is no longer in game… as the only way i have figured out how to check if the entity exists is by using:

if not DoesEntityExist(veh) then

Not sure if any of this would work but, do a loop on one of the client scripts that runs continuously every x seconds or so, do a check on the script so you know if said client is running the session and run the checks on that client who is the host / arbitrator.

So technically:
On a client script
Check for host -> Check if vehicle exists
If it exists, don’t do anything. If it doesn’t exist, create it / spawn it.

Now to check if it exists on the session:
Obviously someone else is now the ‘owner’ of said vehicle, what you could do is run a check on the client script for every client, where you check who owns that vehicle, get the netid, send it to the server and have something on it that broadcasts it to all the other clients. Now when you receive the netid on the other clients, do NetworkRequestControlOfNetworkId(netid) to request control of that netid and after that try setting the blip to that vehicle.

I found even NETWORK_REQUEST_CONTROL_OF_NETWORK_ID was implemented NETWORK_HAS_CONTROL_OF_NETWORK_ID still returned false all the time. Is there any way to get a valid local id of an entity from network id?

Agreed… at this point i have tried everything i can think of… all to no avail.

Have you tried VEHICLE::GET_CLOSEST_VEHICLE? Passing the coordinate together with the network id and using the network id to validate the returned vehicle. (though it is not the direct way)