Jump to content

Помогите исправить скрипт


Recommended Posts

выдает в консоли это

WARNING: [gameplay]/bpcarlocks/car_locks_server.lua:130: Bad argument @ 'getElementData' [Expected element at argument 1]

[2014-07-10 03:23:12] WARNING: [gameplay]/bpcarlocks/car_locks_server.lua:137: Bad 'vehicle' pointer @ 'setVehicleLocked'(1)

вот сам скрипт

-- ================= 
-- Car Locks ( server )
-- by vick.
-- =================
 
-- player element data --
    -- cl_ownedvehicle
-- vehicle element data --
    -- cl_vehicleowner
    -- cl_vehiclelocked
    -- cl_enginestate
 
 
-- resource starts - ends
function initCarLocks ()
    -- Initilize Player Element Data
    local players = getElementsByType ( "player" )
    for k,p in ipairs(players) do
        removeElementData ( p, "cl_ownedvehicle" )
    end
 
    -- Initilize Vehicle Element Data
    local vehicles = getElementsByType ( "vehicle" )
    for k,v in ipairs(vehicles) do
        removeElementData ( v, "cl_vehicleowner" )
        removeElementData ( v, "cl_vehiclelocked" )
        removeElementData ( v, "cl_enginestate" )
        setVehicleLocked ( v, false )
        setVehicleOverrideLights ( v, 0 )
    end
end
addEventHandler ( "onResourceStart", getResourceRootElement ( getThisResource () ), initCarLocks )
addEventHandler ( "onResourceStop", getResourceRootElement ( getThisResource () ), initCarLocks )
 
-- player joins
function cl_PlayerJoin ( )
bindKey ( source, "l", "down", doToggleLocked )
bindKey ( source, "i", "down", doToggleLights )
bindKey ( source, "j", "down", doToggleEngine )
end
addEventHandler ( "onPlayerJoin", getRootElement(), cl_PlayerJoin )
 
-- player quits
function cl_PlayerQuit ( )
    -- check for owned car
    local ownedVehicle = getElementData ( source, "cl_ownedvehicle" )
    if (ownedVehicle ~= false) then
        cl_RemoveVehicleOwner ( ownedVehicle )
    end
end
addEventHandler ( "onPlayerQuit", getRootElement(), cl_PlayerQuit )
 
-- player dies
function cl_PlayerWasted ( )
    -- check for owned car
    local ownedVehicle = getElementData ( source, "cl_ownedvehicle" )
    if (ownedVehicle ~= false) then
        cl_RemoveVehicleOwner ( ownedVehicle )
    end
end
addEventHandler ( "onPlayerWasted", getRootElement(), cl_PlayerWasted )
 
-- player tries to enter vehicle
function cl_VehicleStartEnter ( enteringPlayer, seat, jacked )
    local theVehicle = source
    local theOwner
    -- locked and not owner entering
    if ( getElementData ( theVehicle, "cl_vehiclelocked" ) == true ) then
        theOwner = getElementData ( theVehicle, "cl_vehicleowner" )
        if theOwner ~= false and theOwner ~= enteringPlayer then
            -- make sure they dont enter
            --cancelEvent();
        end
     end
end
addEventHandler ( "onVehicleStartEnter", getRootElement(), cl_VehicleStartEnter )
 
-- player enters a vehicle
function cl_PlayerDriveVehicle ( player, seat, jacked )
    -- Driver Enter
    if ( seat == 0 ) then
        oldVehicle = getElementData ( player, "cl_ownedvehicle" )
        -- not entering player's own owned vehicle
        if ( (cl_VehicleLocked(source) == true) and (cl_VehicleOwner(source) ~= player) ) then
            removePlayerFromVehicle( player )
            Err_Msg("Автомобиль закрыт.", player)
            return false
        end
        -- set element data for vehicle and owner
        cl_SetVehicleOwner ( source, player )
    end
    return true
end
addEventHandler ( "onVehicleEnter", getRootElement(), cl_PlayerDriveVehicle )
 
-- vehicle respawns
function cl_VehicleRespawn ( exploded )
    cl_RemoveVehicleOwner ( source )
end
addEventHandler ( "OnVehicleRespawn", getRootElement(), cl_VehicleRespawn )
 
-- vehicle explosion
function cl_VehicleExplode ( )
    local theOwner = getElementData ( source, "cl_vehicleowner" )
    if ( theOwner ~= false ) then
        cl_RemoveVehicleOwner ( source )
    end
end
addEventHandler ( "onVehicleExplode", getRootElement(), cl_VehicleExplode )
 
-- set vehicle owner
 
function cl_SetVehicleOwner ( theVehicle, thePlayer )
    local oldVehicle = getElementData ( thePlayer, "cl_ownedvehicle" )
    if isElement(oldVehicle) then
        -- unlock old car      
        removeElementData ( oldVehicle, "cl_vehicleowner" )
        removeElementData ( oldVehicle, "cl_vehiclelocked" )
        removeElementData ( oldVehicle, "cl_enginestate" )
        setVehicleLocked ( oldVehicle, false )
        -- set vars for new car
    end
    setElementData ( theVehicle, "cl_vehicleowner", thePlayer )
    setElementData ( theVehicle, "cl_vehiclelocked", false )
    setElementData ( thePlayer, "cl_ownedvehicle", theVehicle )
    setElementData( theVehicle, "cl_enginestate", true )
end
 
function cl_RemoveVehicleOwner ( theVehicle )
    local theOwner = getElementData ( theVehicle, "cl_vehicleowner" )
    if ( theOwner ~= false ) then
        removeElementData ( theOwner, "cl_ownedvehicle" )
        removeElementData ( theVehicle, "cl_vehicleowner" )
        removeElementData ( theVehicle, "cl_vehiclelocked" )
        removeElementData ( owned, "cl_enginestate" )
    end
    setVehicleLocked ( theVehicle, false )
 
end
 
-- flash the lights twice
function cl_FlashLights ( thePlayer )
    setTimer ( doToggleLights, 300, 4, thePlayer, true )
end
 
-- flash once
function cl_FlashOnce ( thePlayer )
    setTimer ( doToggleLights, 300, 2, thePlayer, true )
end
 
-- get vehicle owner ( according to vehicle's element data )
function cl_VehicleOwner ( theVehicle )
    return getElementData( theVehicle, "cl_vehicleowner" )
 
end
-- is vehicle locked ( according to vehicle's element data )
function cl_VehicleLocked ( theVehicle )
    return getElementData( theVehicle, "cl_vehiclelocked" )
end
-- messaging functions
-- send red error message
function Err_Msg ( strout, thePlayer )
    outputChatBox ( strout, thePlayer, 200, 0, 10 )
end
 
-- send message to all occupants of vehicle
function Car_Msg ( strout, theVehicle )
    numseats = getVehicleMaxPassengers ( theVehicle )
    for s = 0, numseats do
        local targetPlayer = getVehicleOccupant ( theVehicle, s )
        if targetPlayer ~= false then
            outputChatBox ( strout, targetPlayer, 30, 144, 255 )
        end
    end
end
-- send aquamarine message to player
function Info_Msg ( strout, thePlayer )
    outputChatBox ( strout, thePlayer, 102, 205, 170 )
end
 
-- commands
function doToggleLocked ( source )
    local theVehicle , strout
    if ( getElementType(source) == "vehicle" ) then
        theVehicle = source
    end
    if ( getElementType(source) == "player" ) then
        theVehicle = getElementData ( source, "cl_ownedvehicle" )
    end
 
    if ( theVehicle ) then
        local vehiclename = getVehicleName ( theVehicle )
        -- already locked
        if ( getElementData ( theVehicle, "cl_vehiclelocked") == true ) then
            doUnlockVehicle ( source )
        else
            doLockVehicle ( source )
        end
    else
        Err_Msg("Вы должны быть в автомобиле, что бы закрыть или открыть его..", source)
    end
end 
 
function doLockVehicle ( source )
    local theVehicle , strout
    if ( getElementType(source) == "vehicle" ) then
        theVehicle = source
    end
    if ( getElementType(source) == "player" ) then
        theVehicle = getElementData ( source, "cl_ownedvehicle" )
    end
 
    if ( theVehicle ) then
        local vehiclename = getVehicleName ( theVehicle )
        -- already locked
        if ( getElementData ( theVehicle, "cl_vehiclelocked") == true ) then
            strout = "Вы снова закрыли " .. vehiclename .. ""
            Err_Msg(strout, source)
        else
            setElementData ( theVehicle, "cl_vehiclelocked", true)
            setVehicleLocked ( theVehicle, true )
            Car_Msg( "Автомобиль " .. vehiclename .. " закрыт.", theVehicle)
            if ( getVehicleController ( theVehicle ) == false ) then
                cl_FlashLights ( source )
            end
        end
    else
        Err_Msg("Вы должны находится в автомобтле.", source)
    end
end
 
function doUnlockVehicle ( source )
    local theVehicle, strout
    if ( getElementType(source) == "vehicle" ) then
        theVehicle = source
    end
    if ( getElementType(source) == "player" ) then
        theVehicle = getElementData ( source, "cl_ownedvehicle" )
    end
    if ( theVehicle ) then
    local vehiclename = getVehicleName ( theVehicle )
        if ( getElementData ( theVehicle, "cl_vehiclelocked") == false ) then
            strout = "Вы " .. vehiclename .. " сново открыли."
            Err_Msg(strout, source)
        else
            setElementData ( theVehicle, "cl_vehiclelocked", false)
            setVehicleLocked ( theVehicle, false )
            Car_Msg( "Автомобиль " .. vehiclename .. " открыт.", theVehicle )
            if ( getVehicleController ( theVehicle ) == false ) then
                cl_FlashOnce ( source )
            end
        end
Link to comment

Всегда проверяй существование элемента посредством специальной функции isElement. Даже если ты удалишь элемент, указатель на него как правило остается в переменной. Поэтому использование проверок на ~= false или ~= nil для элементов неприемлемо.

Link to comment
Всегда проверяй существование элемента посредством специальной функции isElement. Даже если ты удалишь элемент, указатель на него как правило остается в переменной. Поэтому использование проверок на ~= false или ~= nil для элементов неприемлемо.

и как исправить ?

Link to comment
и как исправить ?
... проверяй существование элемента посредством специальной функции isElement ...

Заменяй, потом смотри, что будет выдавать.

не понял что именно заменять...

Link to comment
не понял что именно заменять...

Ну тебе же сказали какая функция нужна. С помощью нее и исправь свои проблемные места в коде, на которые дебаг жалуется.

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