..:D&G:.. Posted May 7, 2016 Posted May 7, 2016 So, I want to get the number of people that are in a vehicle so that I can then compare it to the max passengers the vehicle can hold. I've tried #getVehicleOccupants(vehicle) but it always outputs 0... Any ideas? Thanks.
AlvareZ_ Posted May 7, 2016 Posted May 7, 2016 Im not sure, But try: occupants = getVehicleOccupants(vehicle) -- Get the ocuppants on the car numseats = getVehicleMaxPassengers(getPedOccupiedVehicle ( thePlayer )) -- take the player vehicle if (unpack(ocuppants) == numseats) then -- verify if the vehicle its full
Captain Cody Posted May 7, 2016 Posted May 7, 2016 local occupants = getVehicleOccupants(vehicle) amount = 0 for s, o in pairs(occupants) do local amounta = amount amount = amounta + 1 end
MTA Team botder Posted May 7, 2016 MTA Team Posted May 7, 2016 Note: Don't use an ipairs loop with the table returned by this function. It will skip the driver, as ipairs starts at 1 and the driver seat is ID 0. And if there's an empty seat, ipairs will stop looping. You should use a pairs loop instead. That's why you can't use '#' length operator on the result table.
..:D&G:.. Posted May 7, 2016 Author Posted May 7, 2016 local occupants = getVehicleOccupants(vehicle) amount = 0 for s, o in pairs(occupants) do local amounta = amount amount = amounta + 1 end That worked, thanks.. I have another problem now... Is there a way to find out if there is a free seat in a vehicle and make the ped enter the car as a passenger? This is what I've done and the ped doesn't enter the vehicle even if they are close to it: function makeGirlfriendEnterVehicle(thePlayer, seat, jacked) local vehicle = getPlayerOccupiedVehicle(thePlayer) local occupants = getVehicleOccupants(vehicle) amount = 0 for s, o in pairs(occupants) do local amounta = amount amount = amounta + 1 end local maxPassengers = getVehicleMaxPassengers(vehicle) if not (getElementData(thePlayer, "gfFollow") == nil) then for i, v in ipairs(getElementsByType("ped")) do if (getElementData(v, "name") == getElementData(thePlayer, "hasGirlfriend")) and (getElementData(v, "type") == "girlfriend") then girlFriendPed = v end end if (maxPassengers > amount) then amount = 0 triggerClientEvent ( "girlfriend_EnterCar", girlFriendPed ) end else return end end addEventHandler ( "onVehicleStartEnter", getRootElement(), makeGirlfriendEnterVehicle ) addEvent( "girlfriend_EnterCar", true ) function BEnterCar ( ) if (isElement(source)) then setPedControlState( source, "enter_passenger", true ) end end addEventHandler( "girlfriend_EnterCar", getRootElement(), BEnterCar )
Captain Cody Posted May 7, 2016 Posted May 7, 2016 "enter_passenger" will not work with setPedControlState you would just have to warp the ped into vehicle.
..:D&G:.. Posted May 8, 2016 Author Posted May 8, 2016 Well that kinds sucks... Anyway, is there a method to find a free seat in a vehicle?
Captain Cody Posted May 8, 2016 Posted May 8, 2016 local occupants = getVehicleOccupants(vehicle) amount = 0 for s, o in pairs(occupants) do local amounta = amount amount = amounta + 1 end if (maxPassengers > amount) then
..:D&G:.. Posted May 8, 2016 Author Posted May 8, 2016 Any ideas why I get "C stack overflow" here? for s, o in pairs(occupants) do
Captain Cody Posted May 9, 2016 Posted May 9, 2016 I'm not to sure, I'll have to check it out when I get home.
Moderators IIYAMA Posted May 9, 2016 Moderators Posted May 9, 2016 [quote name=..&G:..]Any ideas why I get "C stack overflow" here? for s, o in pairs(occupants) do You will only get that error when you are calling a function inside this loop, which is related to the function which contains this loop. This is an example how you get this error: function testFunction () testFunction() end testFunction()
..:D&G:.. Posted May 9, 2016 Author Posted May 9, 2016 [quote name=..&G:..]Any ideas why I get "C stack overflow" here? for s, o in pairs(occupants) do You will only get that error when you are calling a function inside this loop, which is related to the function which contains this loop. This is an example how you get this error: function testFunction () testFunction() end testFunction() But there is no function called inside the loop, there is only the local "amount"
Moderators IIYAMA Posted May 9, 2016 Moderators Posted May 9, 2016 You haven't added a wrapper function called pairs in your code?
..:D&G:.. Posted May 9, 2016 Author Posted May 9, 2016 I only use ipairs and this is the other, and only loop is this: amount = 0 for s, o in pairs(occupants) do local amounta = amount amount = amounta + 1 end local maxPassengers = getVehicleMaxPassengers(vehicle) if not (getElementData(thePlayer, "gfFollow") == nil) then for i, v in ipairs(getElementsByType("ped")) do if (getElementData(v, "name") == getElementData(thePlayer, "hasGirlfriend")) and (getElementData(v, "type") == "girlfriend") then girlFriendPed = v end end
..:D&G:.. Posted May 9, 2016 Author Posted May 9, 2016 What is your function called? makeGirlfriendEnterVehicle(thePlayer)
Captain Cody Posted May 10, 2016 Posted May 10, 2016 Try this? function enter (vehicle,thePlayer) passengers = 0 for seat, player in pairs(getVehicleOccupants(vehicle)) do passengers = passengers + 1 end local maxPassengers = getVehicleMaxPassengers(vehicle) if not (getElementData(thePlayer, "gfFollow") == nil) then for i, v in pairs(getElementsByType("ped")) do if (getElementData(v, "name") == getElementData(thePlayer, "hasGirlfriend")) and (getElementData(v, "type") == "girlfriend") then girlFriendPed = v end -- girlFriendPed end end
..:D&G:.. Posted May 10, 2016 Author Posted May 10, 2016 I've noticed that you used "pairs" for that loop, but I always use "ipairs", what's the difference between these two? Lol
Dimos7 Posted May 10, 2016 Posted May 10, 2016 ipairs is slower and stop everythme for somethin pairs not do that
Captain Cody Posted May 10, 2016 Posted May 10, 2016 Ipairs, starts at 1, and stops at the end pairs, randomly goes through them stops when it has looped through it all.
Moderators IIYAMA Posted May 10, 2016 Moderators Posted May 10, 2016 + pairs, loops through custom indexes, like: local tableAsIndex = {} local thisTable = { ["customIndex1"] = 45836, ["customIndex2"] = 45836, [true] = 3246765, [localPlayer] = 547356, [resourceRoot] = 5646745387, [tableAsIndex] = 354654638 -- etc. }
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now