Jump to content

[HELP] Scrip Errors Need help


Recommended Posts

I got script error when i start the resource....

serverSide:

    local pbags = 
        { 
            { 2293.154296875, 561.470703125, 7.78125 }, 
        } 
        local marker = createMarker ( 2291.1999511719, 537, 0.80000001192093, "cylinder", 1.5, 255, 0, 0 ) 
  
    function createBags ( thePlayer ) 
        local x, y, z = unpack ( pbags [ math.random ( #pbags ) ] ) 
        local baga = createPickup ( x, y, z, 3, 1210 ) 
        addEventHandler ("onPickupHit", baga, onPickupHit ) 
        addEventHandler ("onPlayerQuit", baga, onPlayerQuit) 
        addEventHandler ("onMarkerHit", marker, onMarkerHit) 
        addEventHandler ("onResourceStop", getRootElement(), onResourceStop)         
    end 
    addEventHandler ( "onResourceStart", getRootElement(), createBags ) 
      
    function onPickupHit ( thePlayer ) 
        if ( isPedInVehicle ( thePlayer ) ) then 
            return 
        end 
        if ( isPedDead ( thePlayer ) ) then 
            return 
        end 
        local bagb = createObject ( 1210, 0, 0, 0 ) 
        setElementData ( thePlayer, "baga", true ) 
        exports.bone_attach:attachElementToBone(bagb,thePlayer,12,0,0.1,0.3,0,180,0) 
    end 
function onMarkerHit (thePlayer) 
    if getElementData (thePlayer, "baga") and getElementData (thePlayer,"baga") == true then 
    givePlayerMoney (thePlayer, math.random (1000, 2000)) 
    outputChatBox ("You have Earned your Rewards", thePlayer, 0, 255, 0, false) 
    setElementData (thePlayer, "baga", false) 
    exports.bone_attach:detachElementFromBone(bagb) 
    else 
    outputChatBox ("get The Bag First", thePlayer, 255, 0, 0) 
    cancelEvent() 
        end 
    end 
function onPlayerQuit (thePlayer) 
exports.bone_attach:detachElementFromBone(bagb) 
setElementData (thePlayer, "baga", false) 
    end 
  
function onResourceStop (thePlayer) 
exports.bone_attach:detachElementFromBone(bagb) 
setElementData (thePlayer, "baga", false) 
end 

Also the bone attached don't be removed when i hit the marker :/..help please!

Errors:

:46: Bad arguemnt @ 'setElementData' [Expected element at argument 1, got resource-data] 

Link to comment

Like the error says, it received resource-data where it expected a player element.

In this case, thePlayer is returning the resoruce-data because of the way you're adding the event handlers. I'd highly suggest you to add those event handlers outside of a function. As it stands, it's a mess and therefore your issue.

Link to comment
Like the error says, it received resource-data where it expected a player element.

In this case, thePlayer is returning the resoruce-data because of the way you're adding the event handlers. I'd highly suggest you to add those event handlers outside of a function. As it stands, it's a mess and therefore your issue.

Alright when i out the functions i get Errors

:24: Bad Argument @ 'addEventHandler', argument 2 got nil 
:41: bad argument 'addEventHandler' argument 2 got nil 

... i think all functions will get the same because h must add them to onResourceStart...or what do you think?

Link to comment

That's because of how you create the EventHandlers. Let me help you understand error messages, try to understand them instead of simply giving up right away.

:24: Bad Argument @ 'addEventHandler', argument 2 got nil 

1. Okay, so you know the error is on Line 24. If you're using a program such as Notepad++, this will be easy to find.

2. You know the kind of error is a Bad Argument. This means it expected something specific, but it received something else. For example, it was expecting a pickup element, but because you made baga locally inside a function - it doesn't exist outside of that function. Thus, it is returning nil.

Example;

  
local variable1 = "This is a local variable, but created outside a function." 
  
function exampleFunction() 
    local variable2 = "This is a local variable" 
    variable3 = "This is a global variable" 
end 
  
function exampleFunction2() 
    outputChatBox(variable1, 255, 255, 255, true) 
    --This will successfully output the variable, since it was created outside of a function. 
    outputChatBox(variable2, 255, 255, 255, true) 
    --This will fail to output, because variable1 is local to the function above. If you use tostring() it will output nil. 
    outputChatBox(variable3, 255, 255, 255, true) 
    --This will output the string successfully. 
end 

3. Argument 2 is what's causing the error, it expected an element but got nil. Therefore, the error. Argument 1, 2 and 3 in order; ("onPlayerQuit", baga, onPlayerQuit)

This is how I would do it, in my opinion, it's a whole lot easier to keep track of and understand what certain things do. I'd also advice you against naming functions the same as events.

I of course haven't tested this;

local pbags = 
{ 
    {2293.154296875, 561.470703125, 7.78125}, 
} 
local marker = createMarker(2291.1999511719, 537, 0.80000001192093, "cylinder", 1.5, 255, 0, 0) 
local x, y, z = unpack(pbags[math.random(#pbags)]) 
local baga = createPickup(x, y, z, 3, 1210) 
  
function createBags(thePlayer) 
    --Notice how you're creating the locally, this means, it's local to this function. If you try to use it outside of this function, it will return nil. 
    --Thus you should either create it at the top or inside this function without local. 
    --Personally I usually go with the first alternative. 
    --local baga = createPickup(x, y, z, 3, 1210) 
    --baga = createPickup(x, y, z, 3, 1210) 
end 
addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), createBags) 
  
function onPickupHit(thePlayer) 
    if(source == baga) 
        if(isPedInVehicle(southePlayerrce)) then 
            --Are those checks even necessary since you're returning nil? 
            return 
        end 
        if(isPedDead(thePlayer)) then 
            --Are those checks even necessary since you're returning nil? 
            return 
        end 
    local bagb = createObject(1210, 0, 0, 0) 
    setElementData(thePlayer, "baga", true) 
    exports.bone_attach:attachElementToBone(bagb, thePlayer, 12, 0, 0.1, 0.3, 0, 180, 0) 
    end 
end 
addEventHandler("onPickupHit", getRootElement(), onPickupHit) 
  
function onMarkerHit(hitElement, matchingDimension) 
    if(source == marker) then 
        if(getElementData (hitElement, "baga")) and (getElementData(hitElement, "baga") == true) then 
            givePlayerMoney(hitElement, math.random(1000, 2000)) 
            outputChatBox("You have Earned your Rewards", thePlayer, 0, 255, 0, false) 
            setElementData(hitElement, "baga", false) 
            exports.bone_attach:detachElementFromBone(bagb) 
        else 
            outputChatBox ("get The Bag First", hitElement, 255, 0, 0) 
            cancelEvent() 
        end 
    end 
end 
addEventHandler ("onMarkerHit", getRootElement(), onMarkerHit) 
  
function onPlayerQuit(quitType) 
    exports.bone_attach:detachElementFromBone(bagb) 
    setElementData (thePlayer, "baga", false) 
end 
addEventHandler ("onPlayerQuit", getRootElement(), onPlayerQuit) 
  
function onResourceStop(thePlayer) 
    exports.bone_attach:detachElementFromBone(bagb) 
    setElementData(thePlayer, "baga", false) 
end 
addEventHandler ("onResourceStop", getResourceRootElement(getThisResource()), onResourceStop) 

Edited by Guest
Link to comment

as you words.. i have moved the pickup out from the function to create it out...but got Error now on it...and YEs i have notebad++

    local pbags = 
        { 
            { 2293.154296875, 561.470703125, 7.78125 }, 
        } 
        local marker = createMarker ( 2291.1999511719, 537, 0.80000001192093, "cylinder", 1.5, 255, 0, 0 ) 
        local baga = createPickup ( x, y, z, 3, 1210 ) 
    function createBags ( thePlayer ) 
        local x, y, z = unpack ( pbags [ math.random ( #pbags ) ] ) 
  
        end 
addEventHandler ( "onResourceStart", getRootElement(), createBags ) 
      
    function onPickupHit ( thePlayer ) 
        if ( isPedInVehicle ( thePlayer ) ) then 
            return 
        end 
        if ( isPedDead ( thePlayer ) ) then 
            return 
        end 
        local bagb = createObject ( 1210, 0, 0, 0 ) 
        setElementData ( thePlayer, "baga", true ) 
        exports.bone_attach:attachElementToBone(bagb,thePlayer,12,0,0.1,0.3,0,180,0) 
    end 
addEventHandler ("onPickupHit", baga, onPickupHit ) 
function onMarkerHit (thePlayer) 
    if getElementData (thePlayer, "baga") and getElementData (thePlayer,"baga") == true then 
    givePlayerMoney (thePlayer, math.random (1000, 2000)) 
    outputChatBox ("You have Earned your Rewards", thePlayer, 0, 255, 0, false) 
    setElementData (thePlayer, "baga", false) 
    exports.bone_attach:detachElementFromBone(bagb) 
    else 
    outputChatBox ("get The Bag First", thePlayer, 255, 0, 0) 
    cancelEvent() 
        end 
    end 
addEventHandler ("onMarkerHit", marker, onMarkerHit)     
function onPlayerQuit (thePlayer) 
exports.bone_attach:detachElementFromBone(bagb) 
setElementData (thePlayer, "baga", false) 
    end 
    addEventHandler ("onPlayerQuit", baga, onPlayerQuit)     
  
function onResourceStop (thePlayer) 
exports.bone_attach:detachElementFromBone(bagb) 
setElementData (thePlayer, "baga", false) 
end 
addEventHandler ("onResourceStop", getRootElement(), onResourceStop)     

what the problem now?

Link to comment

If you had read my previous replies, you'd of known what is wrong. It is not the same error. It is an entirely different error.

Yet again, I'll help you understand the error in hopes that you'll be able to solve such mistakes by yourself in the future. Since I already described the basics, I'll jump right to it.

1. It received a bad argument when trying to create a pickup. It most likely received nil or a boolean(true/false). In this particular case, it received nil.

2. In this case, it fails to set the co-ordinates "X, Y, Z" because of the same reason I stated in a previous reply.

local baga = createPickup ( x, y, z, 3, 1210 ) 
--Here you try to create a pickup, but X, Y and Z are not yet defined. 
  
function createBags(thePlayer) 
    local x, y, z = unpack(pbags[math.random(#pbags)]) 
    --Here you define X, Y and Z. But you have already tried to create the pickup, therefore that code will not be run again. 
    --Even more so, it would still not be able to use X, Y and Z since they are >>>>local to this function<<<< 
end 

Link to comment
If you had read my previous replies, you'd of known what is wrong. It is not the same error. It is an entirely different error.

Yet again, I'll help you understand the error in hopes that you'll be able to solve such mistakes by yourself in the future. Since I already described the basics, I'll jump right to it.

1. It received a bad argument when trying to create a pickup. It most likely received nil or a boolean(true/false). In this particular case, it received nil.

2. In this case, it fails to set the co-ordinates "X, Y, Z" because of the same reason I stated in a previous reply.

local baga = createPickup ( x, y, z, 3, 1210 ) 
--Here you try to create a pickup, but X, Y and Z are not yet defined. 
  
function createBags(thePlayer) 
    local x, y, z = unpack(pbags[math.random(#pbags)]) 
    --Here you define X, Y and Z. But you have already tried to create the pickup, therefore that code will not be run again. 
    --Even more so, it would still not be able to use X, Y and Z since they are >>>>local to this function<<<< 
end 

Dude...The x, y, z was For the TAble i made for the spawns....understand what i mean? check the script in up.

Link to comment

Anyway i've fixed it...but now i get error here

:41: bad argument @ 'setElementData' element at argument 1, got resource-data  

script now:

    local pbags = 
        { 
            { 2293.154296875, 561.470703125, 7.78125 }, 
        } 
        local marker = createMarker ( 2291.1999511719, 537, 0.80000001192093, "cylinder", 1.5, 255, 0, 0 ) 
        local x, y, z = unpack ( pbags [ math.random ( #pbags ) ] )      
        local baga = createPickup ( x, y, z, 3, 1210 ) 
      
    function onPickupHit ( thePlayer ) 
        if ( isPedInVehicle ( thePlayer ) ) then 
            return 
        end 
        if ( isPedDead ( thePlayer ) ) then 
            return 
        end 
        local bagb = createObject ( 1210, 0, 0, 0 ) 
        setElementData ( thePlayer, "baga", true ) 
        exports.bone_attach:attachElementToBone(bagb,thePlayer,12,0,0.1,0.3,0,180,0) 
    end 
addEventHandler ("onPickupHit", baga, onPickupHit ) 
function onMarkerHit (thePlayer) 
    if getElementData (thePlayer, "baga") and getElementData (thePlayer,"baga") == true then 
    givePlayerMoney (thePlayer, math.random (1000, 2000)) 
    outputChatBox ("You have Earned your Rewards", thePlayer, 0, 255, 0, false) 
    setElementData (thePlayer, "baga", false) 
    exports.bone_attach:detachElementFromBone(bagb) 
    else 
    outputChatBox ("get The Bag First", thePlayer, 255, 0, 0) 
    cancelEvent() 
        end 
    end 
addEventHandler ("onMarkerHit", marker, onMarkerHit)     
function onPlayerQuit (thePlayer) 
exports.bone_attach:detachElementFromBone(bagb) 
setElementData (thePlayer, "baga", false) 
    end 
    addEventHandler ("onPlayerQuit", baga, onPlayerQuit)     
  
function onResourceStop (thePlayer) 
exports.bone_attach:detachElementFromBone(bagb) 
setElementData (thePlayer, "baga", false) 
end 
addEventHandler ("onResourceStop", getRootElement(), onResourceStop)     

Link to comment

You still don't understand how local variables work.

When a local variable is created inside a function, it can not be used outside of that function. Hence why it was fixed when you moved it out.

function onResourceStop (thePlayer) 
exports.bone_attach:detachElementFromBone(bagb) 
setElementData (thePlayer, "baga", false) 
end 
addEventHandler ("onResourceStop", getRootElement(), onResourceStop)   

As for your current error, thePlayer is not defined. thePlayer is currently the resource-data. onResourceStop will only be triggered if the resource is stopped on the server, not for an individual client.

Therefore, you don't need this part of the code as you're doing it when a player quits.

If you however do need to trigger code when the resource is stopped on a client, you'll have to use onClientResourceStop - which obviously is client-side.

Edit:

Another thing is, with your current code, it will be triggered every time ANY resource is stopped. Because you haven't isolated it. If you want to isolate it to the current resource only, you can use this instead of getRootElement();

getResourceRootElement(getThisResource()) 

Link to comment
if(getElementData (hitElement, "baga")) and (getElementData(hitElement, "baga") == true) then 

Doesn't make much sense,

(getElementData(hitElement, "baga") == true) 

should be enough.

From what I can see, "bagb" isn't defined.

whatever i did

    exports.bone_attach:detachElementFromBone(bagb) 

to remove the object from the bone..but not work. in onMarkerHit

Link to comment
Because "bagb" isn't defined anywhere. So it defaults to nil.

Alright. can you Defined it?

Alright i've Defined it..but still not work.. and now when i hit the marker with the bag it says the "else" message...if i haven't the bag it says the 'else message too'..:/ help please.

        local pbags = 
            { 
                { 2293.154296875, 561.470703125, 7.78125 }, 
            } 
            local marker = createMarker ( 2291.1999511719, 537, 0.80000001192093, "cylinder", 1.5, 255, 0, 0 ) 
            local x, y, z = unpack ( pbags [ math.random ( #pbags ) ] )     
            local baga = createPickup ( x, y, z, 3, 1210 ) 
      
        function onPickupHit (thePlayer) 
            if (isPedInVehicle (thePlayer)) then 
                return 
            end 
            if (isPedDead( thePlayer )) then 
                return 
            end 
            local bagb = createObject ( 1210, 0, 0, 0 ) 
            setElementData ( source, "baga", true) 
            setElementData ( source, "bagb", true) 
            exports.bone_attach:attachElementToBone(bagb,thePlayer,12,0,0.1,0.3,0,180,0) 
        end 
    addEventHandler ("onPickupHit", baga, onPickupHit ) 
      
    function onMarkerHit (thePlayer) 
        if (getElementData (source, "baga") == true) and (getElementData (source, "bagb") == true) then 
        setElementData (source, "baga",false) 
        setElementData (source, "bagb",false) 
        givePlayerMoney (thePlayer, math.random (1000, 2000)) 
        outputChatBox ("You've Earned" ..getPlayerMoney(thePlayer).."", thePlayer, 0, 255, 0, false) 
        exports.bone_attach:detachElementFromBone(bagb) 
        else 
      
        outputChatBox ("ERROR: ERROR: Script.", thePlayer, 255, 0, 0) 
        cancelEvent() 
            end 
        end 
    addEventHandler ("onMarkerHit", marker, onMarkerHit) 
        
    function onPlayerQuit () 
    exports.bone_attach:detachElementFromBone(bagb) 
    setElementData (source, "baga", false) 
    setElementData (source, "bagb", false) 
    end 
    addEventHandler ("onPlayerQuit", baga, onPlayerQuit) 
      
        function onResourceStop () 
        setElementData (source, "baga", false) 
        setElementData (source, "bagb", false)  
        end 
        addEventHandler ("onResourceStop", getRootElement(), onResourceStop) 
        
        function onResourceStart () 
        setElementData (source, "baga", false)  
        setElementData (source, "bagb", false) 
        end 
        addEventHandler("onResourceStart", getRootElement(),onResourceStart) 
             
         

Link to comment
local pbags = { 
    {2293.154296875, 561.470703125, 7.78125}, 
} 
  
marker = createMarker(2291.1999511719, 537, 0.80000001192093, "cylinder", 1.5, 255, 0, 0) 
x, y, z = unpack(pbags[math.random(#pbags)])     
baga = createPickup(x, y, z, 3, 1210) 
  
function onPickupHit(player) 
    if source == baga then 
        if not isPedInVehicle(player) and not isPedDead(player) then 
            bagb = createObject(1210, 0, 0, 0) 
            exports.bone_attach:attachElementToBone(bagb,player,12,0,0.1,0.3,0,180,0) 
            destroyElement(source) 
        end 
    end 
end 
addEventHandler("onPickupHit", root, onPickupHit) 
      
function onMarkerHit(player) 
    if getElementType(player) == "player" then 
        local atthedTo = exports.bone_attach:getElementBoneAttachmentDetails(bagb) 
        if atthedTo == player then 
            local money = math.random(1000, 2000) 
            givePlayerMoney(player, money) 
            outputChatBox("You've Earned $"..money, player, 0, 255, 0, false) 
            destroyElement(bagb) 
        end 
    end 
end 
addEventHandler("onMarkerHit", marker, onMarkerHit) 
        
function onPlayerQuit() 
    local atthedTo = exports.bone_attach:getElementBoneAttachmentDetails(bagb) 
    if atthedTo == source then 
        destroyElement(bagb) 
        local x, y, z = getElementPosition(source) 
        baga = createPickup(x, y, z, 3, 1210) 
    end 
end 
addEventHandler("onPlayerQuit", root, onPlayerQuit) 

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