Jump to content

[Resolved]Retrieving car instance from server to clientside.


Recommended Posts

Hi!

So, my client asks the server to spawn a car at a certain position (passed in parameter), and warps the spawning person into it. So far so good, but as it is called through an event (triggerServerEvent), how could I retrieve the instance of the car i just spawned?

Sidenote : I tried to do a "getPedOccupiedVehicle(localPlayer)" right after the event is triggered, but the vehicle doesn't seem to be found, even if it exists.

Here's a codebit to (maybe :P) make it clearer :

CLIENT SIDE :

  
local posTab = { sx, sy, sz, srot } 
triggerServerEvent("requestVehicleSpawn", localPlayer, 448, posTab) 
local vehicle = getPedOccupiedVehicle(localPlayer) 
if not vehicle then 
    outputChatBox("error retrieveing vehicel",255,0,0) 
end 
-- TO DEBUG 
  

The typoed message "error retrieving vehicle" is actually appearing, meaning the vehicle isn't found.

SERVER SIDE EVENT HANDLER:

  
----- SPAWNS REQUESTED VEHICLE AT POSITION 
function spawnVehicle(modelid, position) 
    local car = createVehicle(modelid, position[1], position[2], position[3], 0, 0, position[4]) 
    warpPedIntoVehicle(source, car) 
end 
addEvent("requestVehicleSpawn", true) 
addEventHandler("requestVehicleSpawn", root, spawnVehicle) 
  

I'm probably doing something wrong (maybe even something obvious.. :/), as it doesn't work! :P Forgive me, i'm new!

Thanks in advance!

Edited by Guest
Link to comment

-- client side:

local posTab = { sx, sy, sz, srot } 
triggerServerEvent("requestVehicleSpawn", localPlayer, 448, posTab) 
  
addEvent ( "returnVehicleSpawned", true ) 
addEventHandler ( "returnVehicleSpawned", root, 
    function ( vehicle ) 
        if ( vehicle ) then 
            outputChatBox ( "You've just spawned a: ".. getVehicleName ( vehicle ) ) 
        else 
            outputChatBox ( "Vehicle not sent from server side." ) 
        end 
    end 
) 

-- server side:

----- SPAWNS REQUESTED VEHICLE AT POSITION 
function spawnTheVehicle(modelid, position) -- Never call a function like a default function name. 
    local car = createVehicle(modelid, position[1], position[2], position[3], 0, 0, position[4]) 
    warpPedIntoVehicle(source, car) 
    triggerClientEvent ( source, "returnVehicleSpawned", source, car ) 
end 
addEvent("requestVehicleSpawn", true) 
addEventHandler("requestVehicleSpawn", root, spawnTheVehicle) 

Link to comment

Hmmm tricky, but i guess it could work! Thanks, i'll try right now and report back.

SIDE NOTE: I tried a bit further fixing my problem, and it seems that when i use "getPedOccupiedVehicle()" it doesn't get it right away.

It does like :

1) I spawn a car, then my clientside variable "vehicle" is still empty after "getPedOccupiedVehicle"

2) I spawn another car, then now "vehicle" contains THE FIRST vehicle. Heheh.. I don't know why!

Still i will try your solution, it looks perfect. THANK YOU!

PS: Dr. Who is awesome :)

RESULT: As expected, it works! Thanks again! :)

Edited by Guest
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...