Dzsozi (h03) Posted June 11, 2014 Share Posted June 11, 2014 Hello everyone! I have a problem. I used the wiki's example of enter and exit vehicle. I edited it a little bit, but it's not working. What could be the problem? I don't really understand the tables. Someone could fix this code for me and explain it? I want to create a blip for all the taxis which are being driven. taxiVehicles = { [420]=true, [438]=true } function enterVehicle ( thePlayer, seat, jacked ) if ( taxiVehicles[getElementModel ( source )] ) then taxiBlip = createBlipAttachedTo( taxiVehicles, 0, 3, 255, 255, 0, 255, 0, 999999) end end addEventHandler ( "onVehicleEnter", getRootElement(), enterVehicle ) function exitVehicle ( thePlayer, seat, jacked ) if ( taxiVehicles[getElementModel ( source )] ) then destroyElement(taxiBlip) end end addEventHandler ( "onVehicleExit", getRootElement(), exitVehicle ) P.S.: Sorry for bad English! Link to comment
Cadell Posted June 11, 2014 Share Posted June 11, 2014 try this taxiVehicles = { [420]=true, [438]=true } function enterVehicle ( thePlayer, seat, jacked ) if ( taxiVehicles[getElementModel ( source )] ) then taxiBlip = createBlipAttachedTo( taxiVehicles, 3) end end addEventHandler ( "onVehicleEnter", getRootElement(), enterVehicle ) function exitVehicle ( thePlayer, seat, jacked ) if ( taxiVehicles[getElementModel ( source )] ) then destroyElement(taxiBlip) end end addEventHandler ( "onVehicleExit", getRootElement(), exitVehicle ) Link to comment
Moderators Citizen Posted June 11, 2014 Moderators Share Posted June 11, 2014 No don't listen to him ! His script is as bugged as your original script. Please read and try to understand my comments (both of you). My comments are mainly to make you understand how element datas work for a 1st approach: taxiVehicles = { [420]=true, [438]=true } function enterVehicle ( thePlayer, seat, jacked ) if taxiVehicles[getElementModel( source )] and seat == 0 then -- if seat == 0, then it's the driver seat -- do not use globals for that local taxiBlip = createBlipAttachedTo(source, 0, 3, 255, 255, 0, 255, 0, 999999) setElementData(source, "attachedBlip", taxiBlip) -- Hey vehicle (source) ! I give you something to remember for me, its key is "attachedBlip". Thanks -- so I do not need to store it in a table or something end end addEventHandler ( "onVehicleEnter", root, enterVehicle ) function exitVehicle ( thePlayer, seat, jacked ) if taxiVehicles[getElementModel( source )] then local taxiBlip = getElementData(source, "attachedBlip") -- Hey vehicle (source) ! give me the thing you had to remember for me with the key "attachedBlip". Thanks (and I store his response in taxiBlip local variable so I can use it) if taxiBlip and isElement(taxiBlip) then -- to be sure the thing he gave us is not null or false and is an element destroyElement(taxiBlip) --destroy it end end end addEventHandler ( "onVehicleExit", root, exitVehicle ) A unique variable can hold only one value at a time ! (can be a number, string or mta elements like markers, blips, vehicles, players etc). So imagine, if one guy enter in a taxi, it will create a blip that you are storing into the taxiBlip variable. And while that first guy is still driving it, another player enters in another taxi. It will create another blip, ok, but you will store that other blip in the exact same taxiBlip variable. So now taxiBlip holds the reference to the new created blip. Hmmm yeah and what happened to the first blip's reference ? - It has been thrown away, so you cannot access to that blip anymore (it hasn't been destroyed, so it's still visible on the map). And when the first driver exits his taxi, you are destroying the element stored in taxiBlip variable. But if you are still understanding right, this variable holds the reference to the second blip created. So you are basically destroying the blip of the second player instead of the first one. We can use lua tables to hold every blips created on the taxis and using the vehicle as a key (for a key-value table). But it's a way simpler to use element datas as I shown you. It uses tables internally, so you don't have to mess with those things. Hope it's more clear now. (the blip wasn't created because you used taxiVehicles in createBlipAttachedTo instead of source, which is the vehicle) Link to comment
Dzsozi (h03) Posted June 11, 2014 Author Share Posted June 11, 2014 Thank you very much! Now I understand a little bit better! Thank you again! You helped me alot! Link to comment
Dzsozi (h03) Posted June 11, 2014 Author Share Posted June 11, 2014 But for some reason sometimes the blip is still in the radar. Why is that? For example, I enter in a taxi, the blip is being created, other player enters a taxi, it's created too, but if I exit or delete my taxi the blip is still there. But if an other player is not entering a taxi, and if I exit from mine, the blip is being destroyed. Why is that? EDIT: When someone in the vehicle at the first time and he exits the blip is being destroyed. But if another player (or me) enters in the taxi as a second driver and if he exits the blip is not being destroyed. Why? Link to comment
Dzsozi (h03) Posted June 11, 2014 Author Share Posted June 11, 2014 Oh nevermind, it was my mistake! Thank you again Citizen! Link to comment
Moderators Citizen Posted June 11, 2014 Moderators Share Posted June 11, 2014 No problem 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