Jump to content

Simple question


Outlaw

Recommended Posts

Guys i've made this code and its working very well

weapons = {22,
23,
24,
26,
27,
28,
29,
30,
31,
32,
 }
function sendHeadshot ( attacker, weapon, bodypart, loss )
local hp = getElementHealth(source)
end
	if attacker == getLocalPlayer() then
		if bodypart == 9 and weapon == 34 or weapon == 33 or weapon == 24 or weapon == 25 then
			triggerServerEvent( "onServerHeadshot", getRootElement(), source, attacker, weapon, loss )
			setElementHealth ( source, 0 )
			setPedHeadless( source, true )
		end
	elseif bodypart == 9 and #weapons then

			setElementHealth ( source, hp-50 ) 
	end
end


addEventHandler ( "onClientPedDamage", getRootElement(), sendHeadshot )
addEventHandler ( "onClientPlayerDamage", getRootElement(), sendHeadshot )

The question is:

If i want to check if the weapon is M4 so i write:

if weapon == 31 then

And after using the table, when i wrote #weapons it worked fine but i wonder how did the server knew that #weapons = weapon ?

PS: first time i use tables

Link to comment
  • Scripting Moderators
6 minutes ago, Outlaw said:

Guys i've made this code and its working very well


weapons = {22,
23,
24,
26,
27,
28,
29,
30,
31,
32,
 }
function sendHeadshot ( attacker, weapon, bodypart, loss )
local hp = getElementHealth(source)
end
	if attacker == getLocalPlayer() then
		if bodypart == 9 and weapon == 34 or weapon == 33 or weapon == 24 or weapon == 25 then
			triggerServerEvent( "onServerHeadshot", getRootElement(), source, attacker, weapon, loss )
			setElementHealth ( source, 0 )
			setPedHeadless( source, true )
		end
	elseif bodypart == 9 and #weapons then

			setElementHealth ( source, hp-50 ) 
	end
end


addEventHandler ( "onClientPedDamage", getRootElement(), sendHeadshot )
addEventHandler ( "onClientPlayerDamage", getRootElement(), sendHeadshot )

The question is:

If i want to check if the weapon is M4 so i write:


if weapon == 31 then

And after using the table, when i wrote #weapons it worked fine but i wonder how did the server knew that #weapons = weapon ?

PS: first time i use tables

What you try to achieve?

#weapons -- will return `weapons` table size

If you want to do something when body part is headshot and weapon is in table, then you would need to change a bit a table structure (and condition too), to not loop necessary.

weapons = {
	[22] = true,
	[23] = true,
	[24] = true,
	[26] = true,
	[27] = true,
	[28] = true,
	[29] = true,
	[30] = true,
	[31] = true,
	[32] = true,
}

And condition from:

elseif bodypart == 9 and #weapons then

to:

elseif bodypart == 9 and weapons[weapon] then

So, this way you check if weapon id exists in table, and it's not false and not nil.

If you would change true -> false for a weapon, then this weapon wouldn't count as the one which fulfills the condition - because it would be false.

Link to comment
11 minutes ago, majqq said:

What you try to achieve?


#weapons -- will return `weapons` table size

If you want to do something when body part is headshot and weapon is in table, then you would need to change a bit a table structure (and condition too), to not loop necessary.


weapons = {
	[22] = true,
	[23] = true,
	[24] = true,
	[26] = true,
	[27] = true,
	[28] = true,
	[29] = true,
	[30] = true,
	[31] = true,
	[32] = true,
}

And condition from:


elseif bodypart == 9 and #weapons then

to:


elseif bodypart == 9 and weapons[weapon] then

So, this way you check if weapon id exists in table, and it's not false and not nil.

If you would change true -> false for a weapon, then this weapon wouldn't count as the one which fulfills the condition - because it would be false.

I will try your code it seems much better but my question is:

The word to check the weapon is "weapon",( example: if weapon == 31)  but i'm a bit confused how the" #weapons" considered as "weapon" the 2 words dont look the same

in case instead of weapons table i didn't put the weapons ids, i put bodyparts ex:

bodypartss {3,
  4,
  5,
  6,
  7,
  8,
  9,}

	elseif weapon == 31 and #bodypartss then

How the server will read them?

I'm still new in scripting

Edited by Outlaw
Link to comment
  • Scripting Moderators
31 minutes ago, Outlaw said:

I will try your code it seems much better but my question is:

The word to check the weapon is "weapon",( example: if weapon == 31)  but i'm a bit confused how the" #weapons" considered as "weapon" the 2 words dont look the same

in case instead of weapons table i didn't put the weapons ids, i put bodyparts ex:


bodypartss {3,
  4,
  5,
  6,
  7,
  8,
  9,}

	elseif weapon == 31 and #bodypartss then

How the server will read them?

I'm still new in scripting

It's up to you how you will call them.

weapon

is parameter declared in function arguments.

And 

weapons
-- or
bodypartss

are global tables which could be accessed from every scope of file and other script files declared on same side.

Edited by majqq
Link to comment
1 hour ago, majqq said:

It's up to you how you will call them.


weapon

is parameter declared in function arguments.

And 


weapons
-- or
bodypartss

are global tables which could be accessed from every scope of file and other script files declared on same side.

Dude please focus with me i know that i can change the name of the parametres im talking about #weapons

How did my server knew that #weapons is the parametre(weapon) ? (also there is difference between the words, the parametre is weapon) and my table is weapons (The 's')

Link to comment
  • Scripting Moderators
8 hours ago, Outlaw said:

Dude please focus with me i know that i can change the name of the parametres im talking about #weapons

How did my server knew that #weapons is the parametre(weapon) ? (also there is difference between the words, the parametre is weapon) and my table is weapons (The 's')

#weapons

It will return table size, nothing more.

To check if any weapon exists in table you need to index:

weapon -- of course it can be any other variable, but in this case it should be number.

between brackets:

weapons[ ]

So it will look like:

weapons[weapon] -- you get what i mean?

 

  • Thanks 1
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...