Jump to content

[HELP] Table Error


ViRuZGamiing

Recommended Posts

Hi guys,

as the title says how can I get the marker created in a function to work as event handler.

function startTrainJob (localPlayer) 
    local sv = trainTable 
    for i=1,#sv do 
        local trainTable = sv[i] 
        local vehicle = createVehicle (trainTable["create"][1], trainTable["create"][2], trainTable["create"][3], trainTable["create"][4],trainTable["create"][5], trainTable["create"][6],trainTable["create"][7])  
        setVehicleLocked (vehicle, true) 
        setTrainDirection (vehicle, true) 
        local finishMarker = createMarker (trainTable["marker"][1], trainTable["marker"][2], trainTable["marker"][3], "checkpoint", 6, 255, 150, 0, 255, localPlayer) -- Size 6 
        local finishBlip = createBlip (trainTable["marker"][1], trainTable["marker"][2], trainTable["marker"][3], 56, 2, 255, 0, 0, 255, 0, 10000, localPlayer) 
        setTrainDerailable(vehicle, false) 
         
        trainData[vehicle] = { 
            ["money"]=trainTable["money"], 
            ["spawn"]=trainTable["spawn"], 
            ["marker"]=trainTable["marker"] 
        } 
         
        trainTable["train"] = vehicle 
        trainTable["blip"] = finishBlip 
        trainTable["finishmarker"] = finishMarker 
         
        warpPedIntoVehicle (localPlayer, vehicle) 
    end 
end 
addEvent("startTrainJob", true) 
addEventHandler("startTrainJob", root, startTrainJob, localPlayer) 
  
function getPaid (thePlayer) 
    if data then 
        local price = data["money"] 
        givePlayerMoney (thePlayer, price) 
        destroyElement (unpack(trainTable["finishmarker"])) 
        destroyElement (unpack(trainTable["blip"])) 
        destroyElement (unpack(trainTable["train"])) 
        local spawn = data["spawn"] 
        setElementPosition(thePlayer, unpack(data["spawn"])) 
    end 
end 
  
addEventHandler("onMarkerHit", finishMarker, getPaid) 

No error's except; expected element at arg. 2, got nil

but that's quite normal as it not created yet.

I've also tried to place the event handler in the function but that didn't work.

Regards ViRuZ

Edited by Guest
Link to comment

Other problem showed up;

[17:42:07] WARNING: train-job\server.lua:52: Bad argument @ 'getVehicleOccupant' [Expected vehicle at argument 1, got player] 
[17:42:07] ERROR: train-job\server.lua:61: bad argument #1 to 'unpack' (table expected, got userdata) 

local trainData = {} 
  
local trainTable = { 
    {   --TRAIN 
        ["create"]={538, -1947.7, 140.8, 27.4}, 
        ["money"]=1000, 
        ["spawn"]={602, -1917.5, 296.20001, 41, 0, 0, -180}, 
        ["marker"]={1424.4, 2634.3, 9.8}, 
    }, 
} 
  
function mapTrain () 
    createObject(970, -1958.4, 135.5, 27.2) 
    createObject(970, -1958.4, 140.3, 27.2) 
    createObject(970, -1956.3, 137.9, 27.2, 0, 0, 90) 
    trainGuy = createPed (253, -1958.2, 135.89999, 27.7) 
    setElementRotation(trainGuy, 0, 0, 43) 
end 
  
addEventHandler ( "onResourceStart", resourceRoot, mapTrain ) 
  
function startTrainJob (localPlayer) 
    local sv = trainTable 
    for i=1,#sv do 
        local trainTable = sv[i] 
        local vehicle = createVehicle (trainTable["create"][1], trainTable["create"][2], trainTable["create"][3], trainTable["create"][4],trainTable["create"][5], trainTable["create"][6],trainTable["create"][7])  
        setVehicleLocked (vehicle, true) 
        setTrainDirection (vehicle, true) 
        local finishMarker = createMarker (trainTable["marker"][1], trainTable["marker"][2], trainTable["marker"][3], "checkpoint", 6, 255, 150, 0, 255, localPlayer) -- Size 6 
        addEventHandler("onMarkerHit", finishMarker, getPaid) 
         
        local finishBlip = createBlip (trainTable["marker"][1], trainTable["marker"][2], trainTable["marker"][3], 56, 2, 255, 0, 0, 255, 0, 10000, localPlayer) 
        setTrainDerailable(vehicle, false) 
         
        trainData[vehicle] = { 
            ["money"]=trainTable["money"], 
            ["spawn"]=trainTable["spawn"], 
            ["marker"]=trainTable["marker"], 
            ["train"] = vehicle, 
            ["blip"] = finishBlip, 
            ["finishmarker"] = finishMarker 
        } 
         
        warpPedIntoVehicle (localPlayer, vehicle) 
    end 
end 
addEvent("startTrainJob", true) 
addEventHandler("startTrainJob", root, startTrainJob, localPlayer) 
  
function getPaid (thePlayer) 
local data = trainData[source] 
    if (data) then 
        outputConsole("Data got!") 
        local price = data["money"] 
        local paycheck = givePlayerMoney (thePlayer, price) 
        local sv = trainTable 
        for i=1,#sv do 
            local trainTable = sv[i] 
            destroyElement (unpack(trainTable["finishmarker"])) 
            destroyElement (unpack(trainTable["blip"])) 
            destroyElement (unpack(trainTable["train"])) 
            local spawn = data["spawn"] 
            setElementPosition(thePlayer, unpack(data["spawn"])) 
        end 
    end 
end 

Link to comment

I'm not sure how you managed to get a warning related to 'getVehicleOccupant', since I don't see it inside your script. Not at line 52, at least. As for 'unpack', that's obvious:

trainData[vehicle] = { 
            ["money"]=trainTable["money"], 
            ["spawn"]=trainTable["spawn"], 
            ["marker"]=trainTable["marker"], 
            ["train"] = vehicle, 
            ["blip"] = finishBlip, 
            ["finishmarker"] = finishMarker 
        } 
  
destroyElement (unpack(trainTable["finishmarker"])) 
            destroyElement (unpack(trainTable["blip"])) 
            destroyElement (unpack(trainTable["train"])) 

trainTable["finishmarker"], trainTable["blip"] and trainTable["train"] are elements and you're passing them to 'unpack'.

Also, I don't know what this thing means:

addEventHandler("startTrainJob", root, startTrainJob, localPlayer) 

'localPlayer' is nil on the server-side (unless you assign anything to this variable yourself, of course) and 'addEventHandler' takes a boolean as 4th argument, but this argument is about propagation, and I don't think it's related to whatever you're trying to do.

Link to comment
localPlayer = It comes from Client Side as an trigger argument and works fine

And yes I do have getVehicleOccupant but I added it after I uploaded code. (only thing changed)

The problems is in the tables mainly. I don't understand them 100%

The event handler error is pretty obvious.

Just add the event when the marker is created, or remove the local from it.

Link to comment
localPlayer = It comes from Client Side as an trigger argument and works fine

And yes I do have getVehicleOccupant but I added it after I uploaded code. (only thing changed)

The problems is in the tables mainly. I don't understand them 100%

'localPlayer' outside functions doesn't come from anywhere. It's nil in that line where 'addEventHandler' is called.

And I don't know what tables you're talking about. The problem is exactly not that - 'unpack' takes a table as an argument, but you're passing an element.

Link to comment
  • Moderators

You are writing it and if it is broken you still writing more, so you won't learn a damn about it.

So delete all your code and start again, begin with the startTrainJob function.

I will help you step by step, but only step by step.

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