Jump to content

Little help needed


isa_Khamdan

Recommended Posts

Hello , I have a problem in this code in line 40 unexpected symbol near ) and else I want to make it that if you type the command again it will stop the function.

local theSerial = "0396C6F543425FFE37A326883D73B4F4"  
  
local theCar = getElementData ( getPedOccupiedVehicle ( thePlayer ), "Nexus" ) 
  
addCommandHandler("Colors", 
 function(thePlayer, command) 
  
        for _,player in ipairs(getElementsByType("player")) do 
  
            if getPlayerSerial(player) == theSerial then 
  
                    local car = getPedOccupiedVehicle(player) 
  
                    if getElementModel(car) == theCar then 
        if isVehicleDamageProof(vehicle) == true then 
  
  
                        local color = {} 
  
                        color[1] = math.random(0,126) 
  
                        color[2] = math.random(0,1) 
  
                        color[3] = math.random(0,126) 
  
                        color[4] = math.random(0,126) 
  
                         setVehicleColor ( car, color[1], color[2], color[3], color[4] ) 
  
                    end 
  
                end 
  
            end 
  
        end 
  
   end 
  
);  
  
setTimer( randomVehColors, 2000, 0 ) 

Link to comment
thePlayer isn't defined at getPedOccupiedVehicle function (line 4) and vehicle isn't defined at line 16.
addCommandHandler("start", 
 function(thePlayer, command) 
  
        for _,player in ipairs(getElementsByType("player")) do 
  
            if getPlayerSerial(player) == theSerial then 
  
        if getElementData ( getPedOccupiedVehicle ( thePlayer ), "Nexus" ) then 
                    if isVehicleDamageProof(Nexus) == true then 
  
  
                        local color = {} 
  
                        color[1] = math.random(0,126) 
  
                        color[2] = math.random(0,1) 
  
                        color[3] = math.random(0,126) 
  
                        color[4] = math.random(0,126) 
  
                         setVehicleColor ( car, color[1], color[2], color[3], color[4] ) 
  
                    end 
  
                end 
  
            end 
  
        end 
  
   end 
  
);  
  
setTimer( randomVehColors, 2000, 0 ) 

it tell me that there is error in the timer :S

Link to comment

Well, for the timer problem it's kinda simple since there's no function named 'randomVehColors'. And for the unexpected symbol at line 40, well y'know, I never use semi-colons (;) in lua and I don't see the point of putting one at the end of the function. And I personally don't like the setTimer function, I would suggest you do something with 'onClientRender' and 'getTickCount'. I would show you how but I'm not on my pc atm.

Link to comment
Well, for the timer problem it's kinda simple since there's no function named 'randomVehColors'. And for the unexpected symbol at line 40, well y'know, I never use semi-colons (;) in lua and I don't see the point of putting one at the end of the function. And I personally don't like the setTimer function, I would suggest you do something with 'onClientRender' and 'getTickCount'. I would show you how but I'm not on my pc atm.

Since its only every 2 seconds, I would not use onClientRender. Switching vehicle color's can bring up some client-side lag.

@isa_Khamdan try this;

local theSerial = "0396C6F543425FFE37A326883D73B4F4"  
  
addCommandHandler("Colors", 
    function(thePlayer, command)  
        setTimer( randomVehColors, 2000, 0, thePlayer ); 
    end  
); 
  
function randomVehColors ( thePlayer ) 
    if ( getPlayerSerial( thePlayer ) == theSerial ) then  
        local car = getPedOccupiedVehicle( thePlayer )  
        if ( getElementModel(car) == 560 ) then 
            if ( isVehicleDamageProof(vehicle) == true ) then  
                local color = {}  
                color[1] = math.random(0,126) ; 
                color[2] = math.random(0,126); 
                color[3] = math.random(0,126);  
                color[4] = math.random(0,126) ; 
                setVehicleColor ( car, color[1], color[2], color[3], color[4] )  
            end  
        end  
    end  
end  

Link to comment
Well, for the timer problem it's kinda simple since there's no function named 'randomVehColors'. And for the unexpected symbol at line 40, well y'know, I never use semi-colons (;) in lua and I don't see the point of putting one at the end of the function. And I personally don't like the setTimer function, I would suggest you do something with 'onClientRender' and 'getTickCount'. I would show you how but I'm not on my pc atm.

Since its only every 2 seconds, I would not use onClientRender. Switching vehicle color's can bring up some client-side lag.

@isa_Khamdan try this;

local theSerial = "0396C6F543425FFE37A326883D73B4F4"  
  
addCommandHandler("Colors", 
    function(thePlayer, command)  
        setTimer( randomVehColors, 2000, 0, thePlayer ); 
    end  
); 
  
function randomVehColors ( thePlayer ) 
    if ( getPlayerSerial( thePlayer ) == theSerial ) then  
        local car = getPedOccupiedVehicle( thePlayer )  
        if ( getElementModel(car) == 560 ) then 
            if ( isVehicleDamageProof(vehicle) == true ) then  
                local color = {}  
                color[1] = math.random(0,126) ; 
                color[2] = math.random(0,126); 
                color[3] = math.random(0,126);  
                color[4] = math.random(0,126) ; 
                setVehicleColor ( car, color[1], color[2], color[3], color[4] )  
            end  
        end  
    end  
end  

It works , but can you make it stop when the player leaves the vehicle or if the vehicle is destroyed or anythings like that and I want to stop it if I type the command again

Link to comment

Try this, also added some protection to avoid error's etc;

local theSerial = "0396C6F543425FFE37A326883D73B4F4" 
  
addCommandHandler("Colors", 
    function(thePlayer, command) 
        local v = getPedOccupiedVehicle ( thePlayer ); 
        if ( v ) then 
            if ( isTimer ( vehicleTimer ) ) then  
                killTimer ( vehicleTimer ); 
                return; 
            end 
            vehicleTimer = setTimer( randomVehColors, 2000, 0, thePlayer ); 
        end 
    end 
); 
  
addEventHandler( "onVehicleStartExit", root, 
    function ( thePlayer ) 
        if ( isTimer ( vehicleTimer ) ) then 
            killTimer ( vehicleTimer ); 
        end 
    end 
); 
  
function randomVehColors ( thePlayer ) 
    if ( getPlayerSerial( thePlayer ) == theSerial ) then 
        local car = getPedOccupiedVehicle( thePlayer ) 
        if not ( car ) then 
            if ( isTimer ( vehicleTimer ) ) then 
                killTimer ( vehicletimer ); 
            end 
            return; 
        end 
        if ( getElementModel(car) == 560 ) then 
            if ( isVehicleDamageProof(vehicle) == true ) then 
                local color = {} 
                color[1] = math.random(0,126) ; 
                color[2] = math.random(0,126); 
                color[3] = math.random(0,126); 
                color[4] = math.random(0,126) ; 
                setVehicleColor ( car, color[1], color[2], color[3], color[4] ) 
            end 
        end 
    end 
end 

Link to comment
Try this, also added some protection to avoid error's etc;
local theSerial = "0396C6F543425FFE37A326883D73B4F4" 
  
addCommandHandler("Colors", 
    function(thePlayer, command) 
        local v = getPedOccupiedVehicle ( thePlayer ); 
        if ( v ) then 
            if ( isTimer ( vehicleTimer ) ) then  
                killTimer ( vehicleTimer ); 
                return; 
            end 
            vehicleTimer = setTimer( randomVehColors, 2000, 0, thePlayer ); 
        end 
    end 
); 
  
addEventHandler( "onVehicleStartExit", root, 
    function ( thePlayer ) 
        if ( isTimer ( vehicleTimer ) ) then 
            killTimer ( vehicleTimer ); 
        end 
    end 
); 
  
function randomVehColors ( thePlayer ) 
    if ( getPlayerSerial( thePlayer ) == theSerial ) then 
        local car = getPedOccupiedVehicle( thePlayer ) 
        if not ( car ) then 
            if ( isTimer ( vehicleTimer ) ) then 
                killTimer ( vehicletimer ); 
            end 
            return; 
        end 
        if ( getElementModel(car) == 560 ) then 
            if ( isVehicleDamageProof(vehicle) == true ) then 
                local color = {} 
                color[1] = math.random(0,126) ; 
                color[2] = math.random(0,126); 
                color[3] = math.random(0,126); 
                color[4] = math.random(0,126) ; 
                setVehicleColor ( car, color[1], color[2], color[3], color[4] ) 
            end 
        end 
    end 
end 

Can you change getPedOccupiedVehicle( thePlayer ) to getElementData ( getPedOccupiedVehicle ( thePlayer ), "Nexus" )

Because I when I tried it keep showing errors.

Else there is error with isvehicleDamageProof it show bad argument in debugscript 3

Link to comment

try this, fixed, tested and working;

local theSerial = "CBFE02ADEAD43713EEE5C4F634DA8E13" 
  
addCommandHandler("colors", 
    function(thePlayer, command) 
        outputChatBox("1"); 
        local v = getPedOccupiedVehicle ( thePlayer ); 
        if ( v ) then 
        outputChatBox("2"); 
            if ( isTimer ( vehicleTimer ) ) then 
            outputChatBox("3"); 
                killTimer ( vehicleTimer ); 
                return; 
            end 
            outputChatBox("4"); 
            vehicleTimer = setTimer ( randomVehColors, 2000, 0, thePlayer ); 
        end 
    end 
); 
  
addEventHandler( "onVehicleStartExit", root, 
    function ( thePlayer ) 
        if ( isTimer ( vehicleTimer ) ) then 
            killTimer ( vehicleTimer ); 
        end 
    end 
); 
  
function randomVehColors ( thePlayer ) 
    if ( getPlayerSerial ( thePlayer ) == theSerial ) then 
        local car = getPedOccupiedVehicle( thePlayer ) 
        if not ( car ) then 
            if ( isTimer ( vehicleTimer ) ) then 
                killTimer ( vehicletimer ); 
            end 
            return; 
        end 
        if ( getElementModel( car ) == 560 ) then 
            if ( isVehicleDamageProof( car ) == true ) then 
                local color = {} 
                color[1] = math.random(0,126) ; 
                color[2] = math.random(0,126); 
                color[3] = math.random(0,126); 
                color[4] = math.random(0,126) ; 
                setVehicleColor ( car, color[1], color[2], color[3], color[4] ) 
            end 
        end 
    end 
end 

Link to comment
getElementID returns string not a number.

never mind I removed it

Edit there is one problem in the code it changes the colors only 5-6 times then it stops why?

local theSerial = "0396C6F543425FFE37A326883D73B4F4" 
  
  
  
addCommandHandler("colors", 
  
    function(thePlayer, command) 
  
  
        local v = getPedOccupiedVehicle ( thePlayer ); 
  
        if ( v ) then 
  
  
            if ( isTimer ( vehicleTimer ) ) then 
  
  
                killTimer ( vehicleTimer ); 
  
                return; 
  
            end 
  
  
            vehicleTimer = setTimer ( randomVehColors, 2000, 0, thePlayer ); 
  
        end 
  
    end 
  
); 
  
  
  
addEventHandler( "onVehicleStartExit", root, 
  
    function ( thePlayer ) 
  
        if ( isTimer ( vehicleTimer ) ) then 
  
            killTimer ( vehicleTimer ); 
  
        end 
  
    end 
  
); 
  
  
  
function randomVehColors ( thePlayer ) 
  
    if ( getPlayerSerial ( thePlayer ) == theSerial ) then 
  
        local car = getPedOccupiedVehicle( thePlayer ) 
  
        if not ( car ) then 
  
            if ( isTimer ( vehicleTimer ) ) then 
  
                killTimer ( vehicletimer ); 
  
            end 
  
            return; 
  
        end 
  
            if ( isVehicleDamageProof( car ) == true ) then 
  
                local color = {} 
  
                color[1] = math.random(0,126) ; 
  
                color[2] = math.random(0,126); 
  
                color[3] = math.random(0,126); 
  
                color[4] = math.random(0,126) ; 
  
                setVehicleColor ( car, color[1], color[2], color[3], color[4] ) 
  
            end 
  
        end 
  
    end 

Link to comment
  • Moderators

Try this:

local theSerial = "0396C6F543425FFE37A326883D73B4F4" 
  
  
local vehicleTimer = {} 
  
addCommandHandler("colors", 
  
function(thePlayer, command) 
  
    if ( getPlayerSerial ( thePlayer ) == theSerial ) then 
  
        local v = getPedOccupiedVehicle ( thePlayer ); 
  
        if ( v ) then 
  
  
            if ( isTimer ( vehicleTimer[thePlayer] ) ) then 
  
  
                killTimer ( vehicleTimer[thePlayer] ); 
  
                return; 
  
            end 
  
  
            vehicleTimer[thePlayer] = setTimer ( randomVehColors, 2000, 0, thePlayer ); 
  
        end 
  
    end 
end 
); 
  
  
  
addEventHandler( "onVehicleStartExit", root, 
  
    function ( thePlayer ) 
  
        if ( isTimer ( vehicleTimer[thePlayer] ) ) then 
  
            killTimer ( vehicleTimer[thePlayer] ); 
  
        end 
  
    end 
  
); 
  
  
local color = {} 
  
function randomVehColors ( thePlayer ) 
  
        local car = getPedOccupiedVehicle( thePlayer ) 
  
        if not ( car ) then 
  
            if ( isTimer ( vehicleTimer[thePlayer] ) ) then 
  
                killTimer ( vehicleTimer[thePlayer] ); 
  
            end 
  
            return; 
  
        end 
  
            if ( isVehicleDamageProof( car ) == true ) then 
  
                color[1] = math.random(0,126) ; 
  
                color[2] = math.random(0,126); 
  
                color[3] = math.random(0,126); 
  
                color[4] = math.random(0,126) ; 
  
                setVehicleColor ( car, color[1], color[2], color[3], color[4] ) 
  
            end 
  
end 

Link to comment
Try this:
local theSerial = "0396C6F543425FFE37A326883D73B4F4" 
  
  
local vehicleTimer = {} 
  
addCommandHandler("colors", 
  
function(thePlayer, command) 
  
    if ( getPlayerSerial ( thePlayer ) == theSerial ) then 
  
        local v = getPedOccupiedVehicle ( thePlayer ); 
  
        if ( v ) then 
  
  
            if ( isTimer ( vehicleTimer[thePlayer] ) ) then 
  
  
                killTimer ( vehicleTimer[thePlayer] ); 
  
                return; 
  
            end 
  
  
            vehicleTimer[thePlayer] = setTimer ( randomVehColors, 2000, 0, thePlayer ); 
  
        end 
  
    end 
end 
); 
  
  
  
addEventHandler( "onVehicleStartExit", root, 
  
    function ( thePlayer ) 
  
        if ( isTimer ( vehicleTimer[thePlayer] ) ) then 
  
            killTimer ( vehicleTimer[thePlayer] ); 
  
        end 
  
    end 
  
); 
  
  
local color = {} 
  
function randomVehColors ( thePlayer ) 
  
        local car = getPedOccupiedVehicle( thePlayer ) 
  
        if not ( car ) then 
  
            if ( isTimer ( vehicleTimer[thePlayer] ) ) then 
  
                killTimer ( vehicleTimer[thePlayer] ); 
  
            end 
  
            return; 
  
        end 
  
            if ( isVehicleDamageProof( car ) == true ) then 
  
                color[1] = math.random(0,126) ; 
  
                color[2] = math.random(0,126); 
  
                color[3] = math.random(0,126); 
  
                color[4] = math.random(0,126) ; 
  
                setVehicleColor ( car, color[1], color[2], color[3], color[4] ) 
  
            end 
  
end 

Thank a lot it works great but can you tell me what was the problem so I can avoid it next time?

Edit : else is it possible to stop the timer when you leave the vehicle instead of destroying it?

Link to comment
  • Moderators
Thank a lot it works great but can you tell me what was the problem so I can avoid it next time?

Edit : else is it possible to stop the timer when you leave the vehicle instead of destroying it?

I don't know exactly what was causing that problem, I just edited it to fix some things.

What's wrong with using killTimer? If you want to start the timer again just use setTimer.

Link to comment
Thank a lot it works great but can you tell me what was the problem so I can avoid it next time?

Edit : else is it possible to stop the timer when you leave the vehicle instead of destroying it?

I don't know exactly what was causing that problem, I just edited it to fix some things.

What's wrong with using killTimer? If you want to start the timer again just use setTimer.

I don't want to kill the timer I want to stop it and when you enter the vehicle again it will be resumed , if I set it to kill the timer on exit and set new timer on enter the random colors will be activated without using the command or I am wrong?

Link to comment

the addEventHandler is used to change a pre-defined function of MTA. The function/action you want to change is your first parameter ( right now its 'onVehicleEnter' ). The second parameter is the element that you want to trigger the function, getRootElement() is the same as root; all the elements ingame. The function name is the name of the function where you are editing the action. Like, you want to see if a vehicle is locked, you'll create a function called lockedCheck and give it as the third parameter.

Link to comment
the addEventHandler is used to change a pre-defined function of MTA. The function/action you want to change is your first parameter ( right now its 'onVehicleEnter' ). The second parameter is the element that you want to trigger the function, getRootElement() is the same as root; all the elements ingame. The function name is the name of the function where you are editing the action. Like, you want to see if a vehicle is locked, you'll create a function called lockedCheck and give it as the third parameter.

Hmm can you fix this for me so I can add it to the rest of the code?

addEventHandler("onVehicleEnter", getRootElement(), start); 
  
    if ( getPlayerSerial ( thePlayer ) == theSerial ) then 
  
  
  
        local v = getPedOccupiedVehicle ( thePlayer ); 
  
  
       if ( v ) then 
  
    vehicleTimer[thePlayer] = setTimer ( randomVehColors, 2000, 0, thePlayer ); 
  
   end 
 end 

Link to comment
addEventHandler("onVehicleEnter", getRootElement(), 
    function () 
        if ( getPlayerSerial ( thePlayer ) == theSerial ) then 
            local v = getPedOccupiedVehicle ( thePlayer ); 
            if ( v ) then  
                vehicleTimer[thePlayer] = setTimer ( randomVehColors, 2000, 0, thePlayer );  
            end 
        end 
    end 
); 

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