Jump to content

testing script


Guest

Recommended Posts

just testing myself

what's wrong with this

addEventHandler ("onClientPlayerWeaponFire", root,
function onClientPlayerWeaponFireFunc(weapon, ammo, ammoInClip, hitX, hitY, hitZ, hitElement )
    if weapon == 38 and getElementType(hitElement)=="Vehicle" then
	outputChatBox ( "You are now shooting a car", 255, 0, 0 )
end
end
)

 

Link to comment
addEventHandler("onClientPlayerWeaponFire", root, function(weapon, ammo, ammoInClip, hitX, hitY, hitZ, hitElement)
if weapon == 38 and isElement(source) and isElement(hitElement) and getElementType(source) == "player" and getElementType(hitElement) == "vehicle" then
local r, g, b = getVehicleColor(hitElement)
outputChatBox("".. getVehicleName(hitElement) ..": ".. getPlayerName(source) ..", why you shooting me? :(", r, g, b)
end
end)

:D (untested) but try that. Also, the only problem on your script was the capitalization on "V" when you wrote getElementType(hitElement) == "Vehicle" and for events like that, I don't think is necessary to add a name to it since you won't be calling it from anywhere.

Link to comment
On 7/15/2018 at 12:56, Scarfas said:

just testing myself

what's wrong with this


addEventHandler ("onClientPlayerWeaponFire", root,
function onClientPlayerWeaponFireFunc(weapon, ammo, ammoInClip, hitX, hitY, hitZ, hitElement )
    if weapon == 38 and getElementType(hitElement)=="Vehicle" then
	outputChatBox ( "You are now shooting a car", 255, 0, 0 )
end
end
)

 

Your code is fine it;s just that you have "Vehicle" instead of "vehicle" and you are using a name with an anonymous function.

function function_name( ... )
    -- body
end
addEventHandler( "onClientPedWeaponFire", root, function_name ) -- event not attached to the function so we use a name

addEventHandler( "onClientPedWeaponFire", root,  -- event attached to the function so we do not use a name
    function( ... )
        -- body
    end 
)

 

Link to comment
On 18/07/2018 at 03:40, Mr.Loki said:

Your code is fine it;s just that you have "Vehicle" instead of "vehicle" and you are using a name with an anonymous function.


function function_name( ... )
    -- body
end
addEventHandler( "onClientPedWeaponFire", root, function_name ) -- event not attached to the function so we use a name

addEventHandler( "onClientPedWeaponFire", root,  -- event attached to the function so we do not use a name
    function( ... )
        -- body
    end 
)

 

what about this bro

addEventHandler ("onClientPlayerWeaponFire", root,
function ( weapon, ammo, ammoInClip, hitX, hitY, hitZ, hitElement )
    playerWeapons = { [30]=true, [30]=true}
    local weaponName = getWeaponNameFromID ( playerWeapons )
    if playerWeapons and getElementType(hitElement)=="vehicle" then
	outputChatBox ( "You are now shooting a car with "..playerWeapons.." " )
end
end
)

 

Edited by Guest
Link to comment
local playerWeapons = { [30]=true, [31]=true} 
-- Don't create tables in functions that are meant to be statc.

addEventHandler ("onClientPlayerWeaponFire", root,
    function ( weapon, ammo, ammoInClip, hitX, hitY, hitZ, hitElement )
        if playerWeapons[weapon] and getElementType(hitElement)=="vehicle" then 
        -- this checks if the player weapon is in the table and if the hitElement is a vehicle
            local weaponName = getWeaponNameFromID ( weapon ) 
            -- get the weapon id from the weapon parameter.
            outputChatBox ( "You are now shooting a car with "..weaponName ) 
            -- this is where you put the weapon name you weren't using it before.
        end
    end
)

Not tested tell me if there are errors.

Should work with weapon id 30 and 31

Edited by Mr.Loki
Link to comment

 working, but i will fix it 

i wan't to ask you why you added:

local before > playerWeapons = { [30]=true, [31]=true}

Edited by Guest
Link to comment
11 hours ago, Scarfas said:

local before > playerWeapons = { [30]=true, [31]=true}

Adding local before a variable name turns the var(variable) to a local var which means everything under that local var can access it.

function function_name1( ... )
	print(message)-- error because the message var does not exist.
end
addCommandHandler( "test1", function_name1 )

local message = "hello world"

function function_name2( ... )
	print(message)-- prints "hello world" to the debug to /debugscript 3
end
addCommandHandler( "test2", function_name2 )

If we were to make the message var a global var by removing local the first function would be able  to access it.

Also if you create a local var inside of a function then it will only be visible in that function to everything under it.

function function_name1( ... )
	local message = "hello world"
	print(message)-- prints "hello world" to the debug to /debugscript 3
end
addCommandHandler( "test1", function_name1 )

function function_name2( ... )
	print(message)-- error because the message var does not exist
end
addCommandHandler( "test2", function_name2 )

Another tip is using local variables allow lua to clean up unused vars better with will save memory so don't use global vars unless you have to.

 

 

Edited by Mr.Loki
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...