Jump to content

what the....[help]


mars

Recommended Posts

hey guys,

ive been scripting a mission, but the vehicle doesn't get deleted with this.

dylan2en helped me to make it alitle further, but now the vehicle wont get deleted.

script:

marky = createMarker ( -2642.5, 1375, 6, "cylinder", 1.5, 255, 255, 0, 30 )
 
 
function mission ( hitPlayer )
if (source == marky) then
my = createMarker ( -1647, 1219, 6, "cylinder" , 4, 255, 255, 0, 30 )
setElementVisibleTo ( my, hitPlayer , true )   
local MissionVehicle = createVehicle ( 401, -2639, 1371, 8 )   
warpPedIntoVehicle ( hitPlayer , MissionVehicle )   
blip = createBlip ( -1647, 1219, 4, 52, 2, 255, 0, 0, 255, 0, 99999.0, getRootElement() )
setElementVisibleTo ( blip, hitPlayer, true )
outputChatBox ( "#FF0000[Auto Seller Bob] #00FF00bring this car to otto's dont get to late.", getRootElement(), 255, 255, 255, true )
end
end
addEventHandler("onMarkerHit" , getRootElement(), mission)
 
function Reward( hitPlayer )
if (source == my) then
removePedFromVehicle( hitPlayer)
destroyElement( MissionVehicle )
destroyElement( blip )
destroyElement( my )
givePlayerMoney(hitPlayer, 100000)
outputChatBox ( "#00FF00WELL done, take this as a reward", getRootElement(), 255, 255, 255, true ) 
end
end
addEventHandler ("onMarkerHit" , getRootElement(), Reward)

Link to comment

Your "Reward" function never gets called according to this code. Add an event handler line:

function mission ( hitPlayer )
--if (source == marky) then
   my = createMarker ( -1647, 1219, 6, "cylinder" , 4, 255, 255, 0, 30 )
setElementVisibleTo ( my, hitPlayer , true )   
addEventHandler("onMarkerHit" , my, Reward)
   ...

and a removeEventHandler line:

destroyElement( MissionVehicle )
destroyElement( blip )
destroyElement( my )
removeEventHandler("onMarkerHit" , my, Reward)    
givePlayerMoney(hitPlayer, 100000)

also:

local MissionVehicle = createVehicle ( 401, -2639, 1371, 8 )

MissionVehicle is local to your mission function - it doesn't exist in the scope of the Reward function.

On a side note, people NEED to start learning how to properly attach events to elements.

addEventHandler("onMarkerHit" , getRootElement(), mission)

This will trigger on ANY marker (even ones in other resources), and because of that, you've added the line

if (source == marky) then

YET!

If you just attached the event handler properly, that wouldn't be necessary at all, saving on performance and code efficiency:

addEventHandler("onMarkerHit", marky, mission)

Do NOT attach to the Root Element unless you KNOW you have to! Attach to the Element you are interested in, or the 1st Parent Element of all the Elements you are interested in.

Link to comment

you can scream what ever you want but i'm a newb in lua :oops::oops:

and i do like it! :redhotevil: now i got this:

function destroy ( hitPlayer )

local health = getElementHealth ( MissionVehicle )

local v = getVehicle ( MissionVehicle )

if not v then return; end;

if ( health <= 350 ) then

removePedFromVehicle ( hitPlayer )

destroyElement( v )

destroyElement( blip )

destroyElement( my )

outputChatBox ("#00FF00Your mission failed! , your car exploded.", hitPlayer, 255, 255, 255, true )

end

end

addEventHandler ("onClientRender", getRootElement(), destroy)

addEventHandler ("onMarkerHit" , getRootElement(), Reward)

the problem is, it wont get deleted if the health of the car is less than 350.

Link to comment

Whatever, as you want me to post into this topic I do so. :P

Hmm, you are mixing the clientside instance with the serverside instance.

The reason your code doesnt work is that it never gets called.

"onClientRender" does not exist serverside.

I assume "getVehicle" is your own function somewhere, I never saw this function before in the MTA docu pages.

Before you want to check something you need to think about when the value will change which you want to retrive.

You are trying to retrive damage of a car, when the health reaches under 350 you mark it as "exploded", according to your "mission output".

There is a event which gets triggered at vehicle damage, "onVehicleDamage"...

So you want to check the health, when it reaches a specific value serverside...

addEventHandler("onVehicleDamage", missionVehicle, function(loss)
       local vehicleHealth=getElementHealth(source);
       if (vehicleHealth<=350) then
           print("Your vehicle exploded, you fail.");
           -- Put some handler code here
       end
   end
);

A scope... I know you dont get that word xD

Thats why you made the "uggly" getVehicle function call.

To make code be more viewable... I show you a example on how to save your mission vehicle

local missionVehicle=false;
 
function createMissionVehicle()
   missionVehicle=createVehicle(...);
 
   addEventHandler("onVehicleDamage", missionVehicle, function(loss)
           [...]
       end
   );
   return true;
end

You notice the first line, this line was set to know that there is a variable called "missionVehicle".

I often do this in my code, I set a overview of "everything" in the "headerzone", the headerzone is the zone before "executable code", every function got a headerzone, and all lua "scopes" are function level.

What the [...] is? I guess its self-explanatory, you put the code from above in there, or any of your own handler code.

The event "onVehicleDamage" gets triggered, whenever your mission vehicle gets damaged.

Dont know if the vehicle health is updated already, or if they let you cancel the event to not make it do any damage, just retrive the health of the vehicle and you should be fine I guess... lol.

If you want more details about the event, go into the development docu and look for the serverside event "onVehicleDamage".

I hope you could follow me, in any way, haha :P

If not, feel free to ask questions.

Link to comment

I covered variable scope briefly when writing a quick guide to lua variables in another thread, but here, I was basically just referring to the MissionVehicle element variable had been made local to only his first function. :P

https://forum.multitheftauto.com/viewtop ... 32#p298691

You seem to understand his code structure much better than what he has laid out in this thread, so I shall not help further for now. (I just cannot follow his code if he's not even explained what code sections are running client and serverside!)

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...