Jump to content

Problem with setElementData.


WolfPire

Recommended Posts

Hii! WolfPire over here again.

So guys, today i'm bringing up a really curious case... Which consist in "setElementData" i'm kinda new to this function (As i've never used it before).

And i would like you to please do me 2 big favors that would extend my LUA knowledge (as i'm not too experienced)

1. The Problem:

Server

function lockveh ( thePlayer ) 
    pVehicle = getPedOccupiedVehicle ( thePlayer ) 
    if  isPedInVehicle (thePlayer) == true  then 
        if  isVehicleLocked ( pVehicle )  then 
            if getElementData ( thePlayer, "occupiedVehicle" ) then 
                setVehicleLocked ( pVehicle, false ) 
                outputChatBox ( "Your vehicle has been unlocked.", thePlayer, 0, 255, 0 )    
            else 
                setVehicleLocked ( pVehicle, true ) 
                outputChatBox ( "Your vehicle has been locked.", thePlayer, 255, 0, 0 )    
            end  
        end 
    end 
end 
  
function bindL() 
    occupiedBy = getPlayerFromName(tostring(source)) 
    bindKey ( source, "l", "down", lockveh ) 
    setElementData( source, "occupiedVehicle", occupiedBy ) 
end 
addEventHandler ( "onPlayerVehicleEnter", root, bindL )   

Apparently it wont work for some reason, or i got confused. (No errors in the debug nor console)

____________________________________

2. If you can, can you explain me how "setElementData" and "getElementData" works? I get the idea, but i have no clue how it works or how to use it in a script.

Thanks in advance, guys! =)

Link to comment

Server

function lockveh ( uPlayer ) 
    local uVehicle = getPedOccupiedVehicle ( uPlayer ) 
    if isPedInVehicle ( uPlayer )  then 
        if  isVehicleLocked ( uVehicle )  then 
            if getElementData ( uPlayer, "occupiedVehicle" ) then 
                setVehicleLocked ( uVehicle, false ) 
                outputChatBox ( "Your vehicle has been unlocked.", uPlayer, 0, 255, 0 )    
            else 
                setVehicleLocked ( uVehicle, true ) 
                outputChatBox ( "Your vehicle has been locked.", uPlayer, 255, 0, 0 )    
            end  
        end 
    end 
end 
  
function bindL( ) 
    bindKey ( source, "l", "down", lockveh ) 
    setElementData( source, "occupiedVehicle", getPlayerName( source ) ) 
end 
addEventHandler ( "onPlayerVehicleEnter", root, bindL )   

Example

You use cmd /setSome 1

You use cmd /getSome it returned 1

addCommandHandler( 'setSome', 
    function( player,_,value ) 
        if value then 
            setElementData( player,'some',value ) 
        end  
    end 
)    
  
addCommandHandler( 'getSome', 
    function( player ) 
        outputChatBox( 'getSome -- > '..tostring( getElementData( player,'some' ) ) ) 
    end 
)    

Example 2:

addCommandHandler( 'occupiedVehicles', 
    function( player ) 
        local t = { } 
        for _,v in pairs( getElementsByType 'player' ) do 
            if getElementData( v,'occupiedVehicle' ) then 
                t[ #t + 1 ] = v 
            end  
        end 
        outputChatBox( 'Players occupied vehicles list:\n'..table.concat( t,'\n' ) ) 
        t = nil 
    end 
)    

Example for your code.

GanJaRuleZ said right.

Updated post.

In this situation you not need use setElementData/getElementData.

Link to comment

Well i wanted to use "setElementData" and "getElementData" to get the actual player occupying a vehicle.

So the vehicle will lock ONLY if the player that got in first triggers the function. So it doesn't just get stolen by anoyne else (as i also want it to unlock itself when the owner player gets inside the vehicle)

Thanks GanJa and Kenix =)

But it still doesn't work, my vehicle doesn't get locked and no outputChatBox appears, i'm hoping it's just a condition (if) problem.

Link to comment
function lockveh ( uPlayer ) 
    local uVehicle = getPedOccupiedVehicle ( uPlayer ) 
    if isPedInVehicle ( uPlayer )  then 
        if  not isVehicleLocked ( uVehicle )  then 
            if getElementData ( uPlayer, "occupiedVehicle" ) then 
                setVehicleLocked ( uVehicle, false ) 
                outputChatBox ( "Your vehicle has been unlocked.", uPlayer, 0, 255, 0 )    
            else 
                setVehicleLocked ( uVehicle, true ) 
                outputChatBox ( "Your vehicle has been locked.", uPlayer, 255, 0, 0 )    
            end  
        end 
    end 
end 
  
function bindL( ) 
    bindKey ( source, "l", "down", lockveh ) 
    setElementData( source, "occupiedVehicle", getPlayerName( source ) ) 
end 
addEventHandler ( "onPlayerVehicleEnter", root, bindL )   

Maybe you mean this?

if  not isVehicleLocked ( uVehicle )  then 

If your vehicle not locked? or i not understand you again :/

Updated.

Link to comment
function lockveh ( uPlayer ) 
    local uVehicle = getPedOccupiedVehicle ( uPlayer ) 
    if isPedInVehicle ( uPlayer )  then 
        if  not isVehicleLocked ( uVehicle )  then 
            if getElementData ( uPlayer, "occupiedVehicle" ) then 
                setVehicleLocked ( uVehicle, false ) 
                outputChatBox ( "Your vehicle has been unlocked.", uPlayer, 0, 255, 0 )    
            else 
                setVehicleLocked ( uVehicle, true ) 
                outputChatBox ( "Your vehicle has been locked.", uPlayer, 255, 0, 0 )    
            end  
        end 
    end 
end 
  
function bindL( ) 
    bindKey ( source, "l", "down", lockveh ) 
    setElementData( source, "occupiedVehicle", getPlayerName( source ) ) 
end 
addEventHandler ( "onPlayerVehicleEnter", root, bindL )   

Maybe you mean this?

if  not isVehicleLocked ( uVehicle )  then 

If your vehicle not locked?

Updated.

Actually, no =P

Because as you saw... If all those conditions will be passed, the vehicle will unlock. So, actually it should be like it is...

What i mean with "setElementData" is...

1. Get the player's name.

2. Save it

3. Whenever the players leaves the car, the name is saved.

4. If another player tries to get in the car, it wont be able. (setElementData is not present here)

5. But now if the original player returns to the car. The vehicle will open itself (if locked)

Note: Wierdly i had another experiences with car locks system that whenever you were getting out of the car, you could lock the car and then you couldn't get in again as it would be closed.

The idea is not to let anyone lock the car unless its the vehicle creator =P

Thanks for your patience though =)

Link to comment
function lockveh ( uPlayer ) 
    local uVehicle = getPedOccupiedVehicle ( uPlayer ) 
    if isPedInVehicle ( uPlayer )  then 
        --if  not isVehicleLocked ( uVehicle )  then 
            if getElementData( uVehicle,'occupiedVehicle' )[2] then 
                outputChatBox ( "Your vehicle has been unlocked.", uPlayer, 0, 255, 0 ) 
                setElementData( uVehicle, "occupiedVehicle", { uPlayer,false  } )   
            else 
                outputChatBox ( "Your vehicle has been locked.", uPlayer, 255, 0, 0 )    
                setElementData( uVehicle, "occupiedVehicle", { uPlayer,true } ) 
            end 
        --end 
    end 
end 
  
function bindL( veh ) 
    local data = getElementData( veh,'occupiedVehicle' )[1] 
    if data then 
        if data == source then 
            bindKey ( source, "l", "down", lockveh ) 
        else 
            removePedFromVehicle( source ) 
        end 
    else 
        bindKey ( source, "l", "down", lockveh ) 
    end  
end 
addEventHandler ( "onPlayerVehicleEnter", root, bindL )   
  
addEventHandler ( "onPlayerVehicleExit", root,  
    function( veh )   
        if getElementData( veh,'occupiedVehicle' )[1] == source and getElementData( veh,'occupiedVehicle' )[2] then 
            setVehicleLocked ( uVehicle, true ) 
        end  
    end 
)    

Rewrite again :S

I want sleep.

Edited by Guest
Link to comment
function lockveh ( uPlayer ) 
    local uVehicle = getPedOccupiedVehicle ( uPlayer ) 
    if isPedInVehicle ( uPlayer )  then 
        --if  not isVehicleLocked ( uVehicle )  then 
            if getElementData( uVehicle,'occupiedVehicle' )[2] then 
                outputChatBox ( "Your vehicle has been unlocked.", uPlayer, 0, 255, 0 ) 
                setElementData( uVehicle, "occupiedVehicle", { uPlayer,false  } )   
            else 
                outputChatBox ( "Your vehicle has been locked.", uPlayer, 255, 0, 0 )    
                setElementData( uVehicle, "occupiedVehicle", { uPlayer,true } ) 
            end 
        --end 
    end 
end 
  
function bindL( veh ) 
    local data = getElementData( veh,'occupiedVehicle' )[1] 
    if data then 
        if data == source then 
            bindKey ( source, "l", "down", lockveh ) 
        end 
    else 
        bindKey ( source, "l", "down", lockveh ) 
    end  
end 
addEventHandler ( "onPlayerVehicleEnter", root, bindL )   
  
addEventHandler ( "onPlayerVehicleExit", root,  
    function( veh )   
        if getElementData( veh,'occupiedVehicle' )[1] == source and getElementData( veh,'occupiedVehicle' )[2] then 
            setVehicleLocked ( uVehicle, true ) 
        end  
    end 
)    

Rewrite again :S

Woah!...

And i got 2 errors now...

When the client enters the vehicle: "ERROR: lockONG/lockong.lua:17: attempt to index a boolean value"

When the client exits the vehicle: "ERROR: lockONG/lockong.lua:30: attempt to index a boolean value"

Link to comment
function lockveh ( uPlayer ) 
    local uVehicle = getPedOccupiedVehicle ( uPlayer ) 
    if isPedInVehicle ( uPlayer )  then 
        --if  not isVehicleLocked ( uVehicle )  then 
            if getElementData( uVehicle,'occupiedVehicle' )[2] then 
                outputChatBox ( "Your vehicle has been unlocked.", uPlayer, 0, 255, 0 ) 
                setElementData( uVehicle, "occupiedVehicle", { uPlayer,no  } )   
            else 
                outputChatBox ( "Your vehicle has been locked.", uPlayer, 255, 0, 0 )    
                setElementData( uVehicle, "occupiedVehicle", { uPlayer,yes } ) 
            end 
        --end 
    end 
end 
  
function bindL( veh ) 
    local data = getElementData( veh,'occupiedVehicle' )[1] 
    if data then 
        if data == source then 
            bindKey ( source, "l", "down", lockveh ) 
        end 
    else 
        bindKey ( source, "l", "down", lockveh ) 
    end  
end 
addEventHandler ( "onPlayerVehicleEnter", root, bindL )   
  
addEventHandler ( "onPlayerVehicleExit", root,  
    function( veh )   
        if getElementData( veh,'occupiedVehicle' )[1] == source and getElementData( veh,'occupiedVehicle' )[2] then 
            setVehicleLocked ( uVehicle, true ) 
        end  
    end 
)    

Try.

Link to comment
function lockveh ( uPlayer ) 
    local uVehicle = getPedOccupiedVehicle ( uPlayer ) 
    if isPedInVehicle ( uPlayer )  then 
        --if  not isVehicleLocked ( uVehicle )  then 
            local data = getElementData( uVehicle,'occupiedVehicle' ) 
            if data then 
                outputChatBox ( "Your vehicle has been unlocked.", uPlayer, 0, 255, 0 ) 
                setElementData( uVehicle, "occupiedVehicle", { uPlayer,false  } )   
            else 
                outputChatBox ( "Your vehicle has been locked.", uPlayer, 255, 0, 0 )    
                setElementData( uVehicle, "occupiedVehicle", { uPlayer,true } ) 
            end 
        --end 
    end 
end 
  
function bindL( veh ) 
    local data = getElementData( veh,'occupiedVehicle' ) 
    if data then 
        if data[1] == source then 
            bindKey ( source, "l", "down", lockveh ) 
        else 
            removePedFromVehicle( source ) 
        end 
    else 
        bindKey ( source, "l", "down", lockveh ) 
    end  
end 
addEventHandler ( "onPlayerVehicleEnter", root, bindL )   
  
addEventHandler ( "onPlayerVehicleExit", root,  
    function( veh )   
        local data = getElementData( veh,'occupiedVehicle' ) 
        if data then 
            if data[1] == source and data[2] then 
                setVehicleLocked ( uVehicle, true ) 
            end  
        end  
    end 
)    

I want sleep.
Link to comment
function lockveh ( uPlayer ) 
    local uVehicle = getPedOccupiedVehicle ( uPlayer ) 
    if isPedInVehicle ( uPlayer )  then 
        --if  not isVehicleLocked ( uVehicle )  then 
            local data = getElementData( uVehicle,'occupiedVehicle' ) 
            if data then 
                outputChatBox ( "Your vehicle has been unlocked.", uPlayer, 0, 255, 0 ) 
                setElementData( uVehicle, "occupiedVehicle", { uPlayer,false  } )   
            else 
                outputChatBox ( "Your vehicle has been locked.", uPlayer, 255, 0, 0 )    
                setElementData( uVehicle, "occupiedVehicle", { uPlayer,true } ) 
            end 
        --end 
    end 
end 
  
function bindL( veh ) 
    local data = getElementData( veh,'occupiedVehicle' ) 
    if data then 
        if data[1] == source then 
            bindKey ( source, "l", "down", lockveh ) 
        else 
            removePedFromVehicle( source ) 
        end 
    else 
        bindKey ( source, "l", "down", lockveh ) 
    end  
end 
addEventHandler ( "onPlayerVehicleEnter", root, bindL )   
  
addEventHandler ( "onPlayerVehicleExit", root,  
    function( veh )   
        local data = getElementData( veh,'occupiedVehicle' ) 
        if data then 
            if data[1] == source and data[2] then 
                setVehicleLocked ( uVehicle, true ) 
            end  
        end  
    end 
)    

I want sleep.

Sorry for interrupting your sleeptime >w<

It works now but it has a big major bug...

I press "l" and i get triple "You vehicle has been unlocked."

Link to comment

I kept my word.

Tested.

local bStates = { } 
  
function lockveh ( uPlayer ) 
    if bStates[ uPlayer ] then 
        local uVehicle = getPedOccupiedVehicle ( uPlayer ) 
        if isPedInVehicle ( uPlayer )  then 
            local data = getElementData( uVehicle,'occupiedVehicle' ) 
            if not data or not data[2] then 
                outputChatBox ( "Your vehicle has been locked.", uPlayer, 255, 0, 0 )    
                setElementData( uVehicle, "occupiedVehicle", { uPlayer,true } ) 
                setVehicleLocked ( uVehicle, true ) 
            else 
                outputChatBox ( "Your vehicle has been unlocked.", uPlayer, 0, 255, 0 ) 
                setElementData( uVehicle, "occupiedVehicle", { uPlayer,false  } ) 
                setVehicleLocked ( uVehicle, false )                     
            end 
        end 
    end  
end 
  
function bindL( veh ) 
    local data = getElementData( veh,'occupiedVehicle' ) 
    if data then 
        if data[1] == source then 
            bStates[ source ] = true 
        end 
    else 
        bStates[ source ] = true 
    end  
end 
addEventHandler ( "onPlayerVehicleEnter", root, bindL )  
  
addEventHandler( 'onVehicleStartEnter',root, 
    function( player ) 
        local data = getElementData( source,'occupiedVehicle' ) 
        if data then 
            if isVehicleLocked( source ) and data[1] == player then 
                setVehicleLocked ( source, false ) 
            end 
        end  
    end 
)    
  
addEventHandler( 'onPlayerJoin',root, 
    function( ) 
        bindKey ( source, "l", "down", lockveh ) 
    end 
)    
  
addEventHandler( 'onPlayerQuit',root, 
    function( ) 
        bStates[ source ] = nil 
    end 
)    
  
addEventHandler( 'onResourceStart',root, 
    function( ) 
        for _,v in pairs( getElementsByType 'player' ) do 
            bindKey ( v, "l", "down", lockveh ) 
        end 
    end 
)    
  
addEventHandler ( "onPlayerVehicleExit", root,  
    function( veh )   
        local data = getElementData( veh,'occupiedVehicle' ) 
        if data then 
            if data[1] == source and data[2] then 
                setVehicleLocked ( veh, true ) 
            end  
        end  
        bStates[ source ] = false 
    end 
)    

So if you lock your vehicle then nobody can't enter to vehicle, but only you can enter to vehicle.

If you need explain this just say.

Link to comment
I kept my word.

Tested.

local bStates = { } 
  
function lockveh ( uPlayer ) 
    if bStates[ uPlayer ] then 
        local uVehicle = getPedOccupiedVehicle ( uPlayer ) 
        if isPedInVehicle ( uPlayer )  then 
            local data = getElementData( uVehicle,'occupiedVehicle' ) 
            if not data or not data[2] then 
                outputChatBox ( "Your vehicle has been locked.", uPlayer, 255, 0, 0 )    
                setElementData( uVehicle, "occupiedVehicle", { uPlayer,true } ) 
                setVehicleLocked ( uVehicle, true ) 
            else 
                outputChatBox ( "Your vehicle has been unlocked.", uPlayer, 0, 255, 0 ) 
                setElementData( uVehicle, "occupiedVehicle", { uPlayer,false  } ) 
                setVehicleLocked ( uVehicle, false )                     
            end 
        end 
    end  
end 
  
function bindL( veh ) 
    local data = getElementData( veh,'occupiedVehicle' ) 
    if data then 
        if data[1] == source then 
            bStates[ source ] = true 
        end 
    else 
        bStates[ source ] = true 
    end  
end 
addEventHandler ( "onPlayerVehicleEnter", root, bindL )  
  
addEventHandler( 'onVehicleStartEnter',root, 
    function( player ) 
        local data = getElementData( source,'occupiedVehicle' ) 
        if data then 
            if isVehicleLocked( source ) and data[1] == player then 
                setVehicleLocked ( source, false ) 
            end 
        end  
    end 
)    
  
addEventHandler( 'onPlayerJoin',root, 
    function( ) 
        bindKey ( source, "l", "down", lockveh ) 
    end 
)    
  
addEventHandler( 'onPlayerQuit',root, 
    function( ) 
        bStates[ source ] = nil 
    end 
)    
  
addEventHandler( 'onResourceStart',root, 
    function( ) 
        for _,v in pairs( getElementsByType 'player' ) do 
            bindKey ( v, "l", "down", lockveh ) 
        end 
    end 
)    
  
addEventHandler ( "onPlayerVehicleExit", root,  
    function( veh )   
        local data = getElementData( veh,'occupiedVehicle' ) 
        if data then 
            if data[1] == source and data[2] then 
                setVehicleLocked ( veh, true ) 
            end  
        end  
        bStates[ source ] = false 
    end 
)    

So if you lock your vehicle then nobody can't enter to vehicle, but only you can enter to vehicle.

If you need explain this just say.

Works perfectly.

I found another bug. Which makes it work incorrectly. I can lock it and unlock it once. then it will just only keep unlocking it.

And second, when you get outside and inside the car, it will +1 the unlocking function.

Thanks for your help, Kenix =)

Link to comment

No problem.

I found another bug. Which makes it work incorrectly. I can lock it and unlock it once. then it will just only keep unlocking it.

No i test this if you lock car and exit and again enter then your car unlock.

So you need lock car.

And second, when you get outside and inside the car, it will +1 the unlocking function.

You said if this car owner then unlock car.

Just change the code as you want.

Link to comment
No problem.
I found another bug. Which makes it work incorrectly. I can lock it and unlock it once. then it will just only keep unlocking it.

No i test this if you lock car and exit and again enter then your car unlock.

So you need lock car.

And second, when you get outside and inside the car, it will +1 the unlocking function.

You said if this car owner then unlock car.

Just change the code as you want.

Ook! Thanks anyways =) Sorry for the Trouble c:

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