Jump to content

[Problem] Criminal System


Recommended Posts

I was trying to make a criminal job, but one error keeps coming.

The player has to get the desired car, and take it to the desired location. on the marker hit (DestinationMarker - variable of that in code), the vehicle should get destroyed (not blown up). And his team should be set to criminal.

Marker = createMarker(1946.6300048828, -1804.4787597656, 13.546875,"cylinder", 1, 255, 255, 255, 255) 
local team = createTeam("Criminals", 255, 0, 0) 
local van = createVehicle(482, 1947.1529541016, -1807.6397705078, 13.546875) 
addEvent("criminaljob", true) 
addEventHandler("criminaljob", root, 
function() 
setPlayerTeam(source, getTeamFromName("Criminals")) 
setPlayerNametagColor(source, 255, 0, 0) 
outputChatBox("You accepted the job Criminal. Take the van to the yellow blip in map.", source, 0, 255, 0) 
end 
) 
  
addEventHandler("onMarkerHit", Marker, 
function(plr) 
triggerClientEvent(plr, "onHit", plr) 
end 
) 
  
function onEnter(vehicle) 
if isPedInVehicle(source) and vehicle == van then 
outputChatBox("Take the van to the desired location to become a criminal! (Blip)", source, 255, 0, 0) 
local blip = createBlip(1997.4537353516, -1829.6383056641, 13.546875, 0) 
setElementVisibleTo(blip, source, true) 
local DestinationMarker = createMarker(1997.4537353516, -1829.6383056641, 13.546875,"cylinder", 1, 255, 0, 0, 255) 
setElementVisibleTo(DestinationMarker, source, true) 
else 
return false 
end 
end 
addEventHandler("onPlayerVehicleEnter", root, onEnter) 
  
addEventHandler("onMarkerHit", DestinationMarker, 
function(vehicle) 
if not isPedInVehicle(source) then 
return false 
end 
if not vehicle == van then 
return false 
end 
local vehicle = getPedOccupiedVehicle(source) 
destroyElement(vehicle) 
outputChatBox("You reached the desired destination, you're now a criminal!", source, 255, 0, 0) 
setPlayerTeam(source, getTeamFromName("Criminals")) 
end 
) 

Debugscript 3 Error: Lua: 32 - Expected element at argument 2 got nil.

Can anybody fix my code? Can't find out how it has to be fixed. Thanks!

Link to comment

done

function onEnter(vehicle) 
if isPedInVehicle(source) and vehicle == van then 
outputChatBox("Take the van to the desired location to become a criminal! (Blip)", source, 255, 0, 0) 
local blip = createBlip(1997.4537353516, -1829.6383056641, 13.546875, 0) 
setElementVisibleTo(blip, source, true) 
DestinationMarker = createMarker(1997.4537353516, -1829.6383056641, 13.546875,"cylinder", 1, 255, 0, 0, 255) 
setElementVisibleTo(DestinationMarker, source, true) 
else 
return false 
end 
end 
addEventHandler("onPlayerVehicleEnter", root, onEnter) 

local DestinationMarker it's a local variable so you must remove 'local' or add the event "onMarkerHit" iside onEnter function.

Note : many things wrong in your code.

Link to comment

Edit: Oh, a response is already there, oh well - I'll leave my response just for the record.

Original message:

DestinationMarker isn't defined at that point in the script - it is defined during a function which is run at a later point. At this stage I would also like to point out that DestinationMarker is defined locally within the function - that means it won't be defined outside, thus no other function will be able to confirm that this marker was hit, and not a different one. You should make that a global variable (or local for the file, by changing it into a normal global variable, but adding

local DestinationMarker 

at the beginning of the file).

Try setting the attach to resourceRoot (everything that belongs to this resource) and then in that function which handles the event, make a check to see what was hit.

addEventHandler("onMarkerHit", resourceRoot, 
function(vehicle) 
if not isPedInVehicle(source) then 
return false 
end 
if not vehicle == van then 
return false 
end 
if not source == DestinationMarker then --Make sure that the DestinationMarker was hit, not anything else 
return false 
end 

Link to comment

Thanks MrTasty, that problem is fixed. Another error occured.

Lua 34 : Bad Argument @ isPedInVehicle [Expected ped at argument 1, got marker]

Latest code:

Marker = createMarker(1946.6300048828, -1804.4787597656, 13.546875,"cylinder", 1, 255, 255, 255, 255) 
DestinationMarker = createMarker(1997.4537353516, -1829.6383056641, 13.546875,"cylinder", 1, 255, 0, 0, 255) 
local team = createTeam("Criminals", 255, 0, 0) 
local van = createVehicle(482, 1947.1529541016, -1807.6397705078, 13.546875) 
addEvent("criminaljob", true) 
addEventHandler("criminaljob", root, 
function() 
setPlayerTeam(source, getTeamFromName("Criminals")) 
setPlayerNametagColor(source, 255, 0, 0) 
outputChatBox("You accepted the job Criminal. Take the van to the yellow blip in map.", source, 0, 255, 0) 
end 
) 
  
addEventHandler("onMarkerHit", Marker, 
function(plr) 
triggerClientEvent(plr, "onHit", plr) 
end 
) 
  
function onEnter(vehicle) 
if isPedInVehicle(source) and vehicle == van then 
outputChatBox("Take the van to the desired location to become a criminal! (Blip)", source, 255, 0, 0) 
local blip = createBlip(1997.4537353516, -1829.6383056641, 13.546875, 0) 
setElementVisibleTo(blip, source, true) 
setElementVisibleTo(DestinationMarker, source, true) 
else 
return false 
end 
end 
addEventHandler("onPlayerVehicleEnter", root, onEnter) 
  
addEventHandler("onMarkerHit", resourceRoot, 
function(vehicle) 
if not isPedInVehicle(source) then 
return false 
end 
if not vehicle == van then 
return false 
end 
if not source == DestinationMarker then 
return false 
end 
local vehicle = getPedOccupiedVehicle(source) 
destroyElement(vehicle) 
outputChatBox("You reached the desired destination, you're now a criminal!", source, 255, 0, 0) 
setPlayerTeam(source, getTeamFromName("Criminals")) 
end 
) 

Link to comment

Anyways Fixed the whole code

local Marker = createMarker(1946.6300048828, -1804.4787597656, 13.546875,"cylinder", 1, 255, 255, 255, 255) 
local team = createTeam("Criminals", 255, 0, 0) 
local van = createVehicle(482, 1947.1529541016, -1807.6397705078, 13.546875) 
  
addEvent("criminaljob", true) 
addEventHandler("criminaljob", resourceRoot, 
function() 
    setPlayerTeam(client, getTeamFromName("Criminals")) 
    setPlayerNametagColor(client, 255, 0, 0) 
    outputChatBox("You accepted the job Criminal. Take the van to the yellow blip in map.",client, 0, 255, 0) 
end 
) 
  
addEventHandler("onMarkerHit", Marker, 
function(hitElement,matchingDimension) 
    if matchingDimension and isElement(hitElement) and getElementType(hitElement) == "player" then 
    triggerClientEvent(hitElement, "onHit", hitElement) 
    end  
end 
) 
  
function onEnter(thePlayer, seat, jacked ) 
    if source == van then 
        outputChatBox("Take the van to the desired location to become a criminal! (Blip)",thePlayer, 255, 0, 0) 
        local blip = createBlip(1997.4537353516, -1829.6383056641, 13.546875, 0) 
        setElementVisibleTo(blip,thePlayer, true) 
        DestinationMarker = createMarker(1997.4537353516, -1829.6383056641, 13.546875,"cylinder", 1, 255, 0, 0, 255) 
        setElementVisibleTo(DestinationMarker, source, true) 
    end 
end 
addEventHandler ( "onVehicleEnter", getRootElement(), onEnter ) 
  
addEventHandler("onMarkerHit", DestinationMarker, 
function(hitElement,matchingDimension) 
    if matchingDimension and isElement(hitElement) and getElementType(hitElement) == "player" then  
        if not isPedInVehicle(hitElement) then return end 
            local vehicle = getPedOccupiedVehicle(hitElement) 
                if vehicle then  
                    if  vehicle == van then 
                destroyElement(vehicle) 
            outputChatBox("You reached the desired destination, you're now a criminal!", source, 255, 0, 0) 
        setPlayerTeam(source, getTeamFromName("Criminals")) 
        end  
    end  
end  
end 
) 

Untested

Link to comment

Still error coming, Walid.

Well, MrTasty's tip worked, and another error came. This is my latest code ( I mentioned above as well ).

Marker = createMarker(1946.6300048828, -1804.4787597656, 13.546875,"cylinder", 1, 255, 255, 255, 255) 
DestinationMarker = createMarker(1997.4537353516, -1829.6383056641, 13.546875,"cylinder", 1, 255, 0, 0, 255) 
local team = createTeam("Criminals", 255, 0, 0) 
local van = createVehicle(482, 1947.1529541016, -1807.6397705078, 13.546875) 
addEvent("criminaljob", true) 
addEventHandler("criminaljob", root, 
function() 
setPlayerTeam(source, getTeamFromName("Criminals")) 
setPlayerNametagColor(source, 255, 0, 0) 
outputChatBox("You accepted the job Criminal. Take the van to the yellow blip in map.", source, 0, 255, 0) 
end 
) 
  
addEventHandler("onMarkerHit", Marker, 
function(plr) 
triggerClientEvent(plr, "onHit", plr) 
end 
) 
  
function onEnter(vehicle) 
if isPedInVehicle(source) and vehicle == van then 
outputChatBox("Take the van to the desired location to become a criminal! (Blip)", source, 255, 0, 0) 
local blip = createBlip(1997.4537353516, -1829.6383056641, 13.546875, 0) 
setElementVisibleTo(blip, source, true) 
setElementVisibleTo(DestinationMarker, source, true) 
else 
return false 
end 
end 
addEventHandler("onPlayerVehicleEnter", root, onEnter) 
  
addEventHandler("onMarkerHit", resourceRoot, 
function(vehicle) 
if not isPedInVehicle(source) then 
return false 
end 
if not vehicle == van then 
return false 
end 
if not source == DestinationMarker then 
return false 
end 
local vehicle = getPedOccupiedVehicle(source) 
destroyElement(vehicle) 
outputChatBox("You reached the desired destination, you're now a criminal!", source, 255, 0, 0) 
setPlayerTeam(source, getTeamFromName("Criminals")) 
end 
) 

Lua 34 : Bad Argument @ isPedInVehicle [Expected ped at argument 1, got marker]

Link to comment

Man use my code .

About this

Lua 34 : Bad Argument @ isPedInVehicle [Expected ped at argument 1, got marker]

simply because MrTasty gave you a wrong code did you know why because the source of this event "onMarkerHit" is the marker that got hited by the element.

so in your code you are checking if the marker is inside a vehicle. is that normal ???

Edited by Guest
Link to comment
Walid, if I use your code. Then error comes.

Lua 33: Expected element at argument 2 got nil

it's very simple put the marker outside like this

local DestinationMarker = createMarker(1997.4537353516, -1829.6383056641, 13.546875,"cylinder", 1, 255, 0, 0, 255) 

or simply put the eventHandler 'onMarkerHit' inside the function like this:

function onEnter(thePlayer, seat, jacked ) 
    if source == van then 
        outputChatBox("Take the van to the desired location to become a criminal! (Blip)",thePlayer, 255, 0, 0) 
        local blip = createBlip(1997.4537353516, -1829.6383056641, 13.546875, 0) 
        local DestinationMarker = createMarker(1997.4537353516, -1829.6383056641, 13.546875,"cylinder", 1, 255, 0, 0, 255) 
        setElementVisibleTo(blip,thePlayer, true) 
        setElementVisibleTo(DestinationMarker, thePlayer, true) 
        addEventHandler("onMarkerHit", DestinationMarker,check) 
    end 
end 
addEventHandler ( "onVehicleEnter", getRootElement(), onEnter ) 
  
  
function check(hitElement,matchingDimension) 
    if matchingDimension and isElement(hitElement) and getElementType(hitElement) == "player" then  
        if not isPedInVehicle(hitElement) then return end 
            local vehicle = getPedOccupiedVehicle(hitElement) 
                if vehicle then  
                    if  vehicle == van then 
                destroyElement(vehicle) 
            outputChatBox("You reached the desired destination, you're now a criminal!", source, 255, 0, 0) 
        setPlayerTeam(source, getTeamFromName("Criminals")) 
        end  
    end  
end  
end 

copy and past my code because i'm pretty sure you still using DestinationMarker as a local variable.

BTW why you used setPlayerTeam two times because the player is already criminal.

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