Jump to content

Vehicle Lights Toggle.


DazzaJay

Recommended Posts

i found in my random stuffing around a vehicle lights toggle in fr.lua.

so i decided to copy those lines out into thier own script so it runs all the time..... problem is, ive boned it up somewhere..... and without knowing a thing about LUA, what have i done wrong......

when i load the script i get the error "WARNING: vehiclelights.lua: Bad argument @ 'bindKey' - Line: 8

local root = getRootElement () 
local thisResourceRoot = getResourceRootElement(getThisResource()) 
  
function thisResourceStart () 
    outputChatBox ( "Headlight Switch loaded! Press L to toggle your car lights on / off!" ) 
    local players = getElementsByType ( "player" ) 
    for k,v in ipairs(players) do 
        bindKey ( v, "l", "down", "Lights on/off", toggleVehicleLights  ) 
    end 
end 
  
function playerJoin () 
    outputChatBox ( "You can press L to toggle your car lights on / off!", source ) 
    bindKey ( source, "l", "down", toggleVehicleLights, "Lights on/off" ) 
end 
  
addEventHandler ( "onResourceStart", thisResourceRoot, thisResourceStart ) 
addEventHandler ( "onPlayerJoin", root, playerJoin ) 
  
function toggleVehicleLights ( player, key, state ) 
    if ( getPlayerOccupiedVehicleSeat ( player ) == 0 ) then 
        local veh = getPlayerOccupiedVehicle ( player ) 
        if ( getVehicleOverrideLights ( veh ) ~= 2 ) then 
            setVehicleOverrideLights ( veh, 2 ) 
        else 
            setVehicleOverrideLights ( veh, 1 ) 
        end 
    end 
end 

Link to comment

its possible, i just went in to test if it worked in the original fr.lua file... and i got this on load....

WARNING: fr.lua: Bad argument @ 'bindKey' - Line: 8

WARNING: fr.lua: Bad argument @ 'bindKey' - Line: 9

so, if DP2 has that broken, then its possible that the script is fine.. but DP2 isnt?

Link to comment

        bindKey ( v, "l", "down", "Lights on/off", toggleVehicleLights  ) 

The syntax of that function is incorrect, the bindKey() function takes the arguments as follows:

bindKey ( player thePlayer, string key, string keyState, function handlerFunction, [ var arguments, ... ] )

Therefore, you should change the code in the codeblock to:

        bindKey ( v, "l", "down", toggleVehicleLights, "Lights on/off"  ) 

And yes, this was also wrong in fr.lua (look at the one in your playerjoin event function, that one is correct).

Link to comment

and for anyone else who wants this pointless script....

this is the vehiclelights.jua file

local root = getRootElement () 
local thisResourceRoot = getResourceRootElement(getThisResource()) 
  
function thisResourceStart () 
    outputChatBox ( "Headlight Switch loaded! Press L to toggle your car lights on / off!" ) 
    local players = getElementsByType ( "player" ) 
    for k,v in ipairs(players) do 
        bindKey ( v, "l", "down", toggleVehicleLights, "Lights on/off" ) 
    end 
end 
  
function playerJoin () 
    outputChatBox ( "You can press L to toggle your car lights on / off!", source ) 
    bindKey ( source, "l", "down", toggleVehicleLights, "Lights on/off" ) 
end 
  
addEventHandler ( "onResourceStart", thisResourceRoot, thisResourceStart ) 
addEventHandler ( "onPlayerJoin", root, playerJoin ) 
  
function toggleVehicleLights ( player, key, state ) 
    if ( getPlayerOccupiedVehicleSeat ( player ) == 0 ) then 
        local veh = getPlayerOccupiedVehicle ( player ) 
        if ( getVehicleOverrideLights ( veh ) ~= 2 ) then 
            setVehicleOverrideLights ( veh, 2 ) 
        else 
            setVehicleOverrideLights ( veh, 1 ) 
        end 
    end 
end 
  

this is the meta.xml

<meta> 
    <script src="vehiclelights.lua" /> 
    <info author="Ripped from fr by DazzaJay" /> 
</meta> 

put them two files into vehiclelights.zip file and place in your servers \mods\deathmatch\resources\ folder.

To have this start when the server is loaded....

put this in your mtaserver.conf

   <resource src="vehiclelights" startup="1" protected="0"/> 

-----------------------------------------------------------

If you want the ability to Lock / unlock your cars doors, use this vehiclelights.lua file instead of the one above....

local root = getRootElement () 
local thisResourceRoot = getResourceRootElement(getThisResource()) 
  
function thisResourceStart () 
    outputChatBox ( "Headlight Switch loaded! Press L to toggle your car lights on / off!" ) 
    local players = getElementsByType ( "player" ) 
    for k,v in ipairs(players) do 
    bindKey ( v, "k", "down", toggleVehicleLock, "Lock/unlock door" ) 
        bindKey ( v, "l", "down", toggleVehicleLights, "Lights on/off" ) 
    end 
end 
  
function playerJoin () 
    outputChatBox ( "You can press L to toggle your car lights on / off!", source ) 
    bindKey ( source, "l", "down", toggleVehicleLights, "Lights on/off" ) 
    bindKey ( source, "k", "down", toggleVehicleLock, "Lock/unlock door" ) 
end 
  
addEventHandler ( "onResourceStart", thisResourceRoot, thisResourceStart ) 
addEventHandler ( "onPlayerJoin", root, playerJoin ) 
  
function toggleVehicleLights ( player, key, state ) 
    if ( getPlayerOccupiedVehicleSeat ( player ) == 0 ) then 
        local veh = getPlayerOccupiedVehicle ( player ) 
        if ( getVehicleOverrideLights ( veh ) ~= 2 ) then 
            setVehicleOverrideLights ( veh, 2 ) 
        else 
            setVehicleOverrideLights ( veh, 1 ) 
        end 
    end 
end 
function toggleVehicleLock ( player, key, state ) 
    if ( getPlayerOccupiedVehicleSeat ( player ) == 0 ) then         
        local veh = getPlayerOccupiedVehicle ( player ) 
        if ( isVehicleLocked ( veh ) ) then 
            setVehicleLocked ( veh, false ) 
        else 
            setVehicleLocked ( veh, true ) 
        end 
    end 
end 
  

Link to comment
Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...