Rob0 Posted November 20, 2015 Share Posted November 20, 2015 When running this script from here: https://wiki.multitheftauto.com/wiki/IsVehicleOccupied I often get this error from this script which works perfectly most times, until it doesn't: [2015-11-19 23:59:18] ERROR: [gta3sa]test.lua:3: bad argument #1 to 'next' (table expected, got boolean) It seems to result in this error when confronted with particular vehicles some of similar models. for example it will run through a table of 20 vehicles in the cuboid, then when i add a baggage with 3 trailers it errors, or does the same due to a sentinel being in the list. I don't know the "next" function. Does anything look wrong with this example? any ideas why it errors sometimes but not others. is there another script or method that i might use that performs the same function? function isVehicleOccupied(vehicle) assert(isElement(vehicle) and getElementType(vehicle) == "vehicle", "Bad argument @ isVehicleOccupied [expected vehicle, got " .. tostring(vehicle) .. "]") local _, occupant = next(getVehicleOccupants(vehicle)) return occupant and true, occupant end Link to comment
Moderators IIYAMA Posted November 20, 2015 Moderators Share Posted November 20, 2015 'next' returns the first data and it's index of a table if there is. The reason why the code fails is because the vehicle is nil/false. Which causes getVehicleOccupants to fail working(you also got an warning of that) and will return nil... next(nil) = ERROR. So... Replace line 2 with: if not isElement(vehicle) or getElementType(vehicle) ~= "vehicle" then return false, nil end Link to comment
Rob0 Posted November 21, 2015 Author Share Posted November 21, 2015 Thanks, but didn't work. though it did lead me to a workaround by excepting id ~= 607 which is the vehicle it typically errors on. which is odd 606, the other baggage trailer, passes. I'll check back for a fix, until such time this looks like it might work. Link to comment
myonlake Posted November 21, 2015 Share Posted November 21, 2015 Trailers fail because they have no seats. If you read what getVehicleOccupants results you'd know that it returns false when there's no seats or vehicle element is invalid. function isVehicleOccupied(vehicle) assert(isElement(vehicle) and getElementType(vehicle) == "vehicle", "Bad argument @ isVehicleOccupied [expected vehicle, got " .. tostring(vehicle) .. "]") local _, occupant = next(getVehicleOccupants(vehicle) or {}) return occupant and true, occupant end Link to comment
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