Jump to content

How do add ID for vehs/players


Recommended Posts

local players = getElementsByType('player')
local vehicles = getElementsByType('vehicle')
-- then just use index's, like this
if player[1] then -- 1, 2, 3, 4, 5 etc inside of [], this number will be your index or your 'id'
	outputChatBox(getPlayerName(player[1]))
end

 

  • Thanks 1
Link to post
16 minutes ago, ShayF said:

local players = getElementsByType('player')
local vehicles = getElementsByType('vehicle')
-- then just use index's, like this
if player[1] then -- 1, 2, 3, 4, 5 etc inside of [], this number will be your index or your 'id'
	outputChatBox(getPlayerName(player[1]))
end

 

Thank but how do with object? Example;((MissionComp create a object(ID25))) i want to with thats id i can delete object  from id how do? @ShayF

Link to post
-- this is for individual objects if u want to count only the ones u create

local objects = {}
local _obj = createObject
Object = createObject

function createObject(...)
	local obj = _obj(...)
	objects[#objects+1] = obj
	return obj
end
-- then you would use index's on objects table like before
if objects[1] then
	print(getElementPosition(objects[1]))
end

-- this is for if you want to grab all objects
local objects = getElementsByType('object')
-- then use index's like I said before
if objects[1] then
	print(getElementPosition(objects[1]))
end

 

Edited by ShayF
Link to post

I think you first need to learn what table index's and table keys are, @MissionComp.
Table index's are the positions inside the table, and using such index can allow you to grab things from a specific spot in the table.
Index's go up from 0, and they go to like an infinite amount.
Say you have a table:

local t = {}

Now lets put something in the table:

table.insert(t, 'Hello World')

Okay so we've put a string that says Hello World into the table. Now the table looks like this, without you having to recreate it again:

 t = {'Hello World'}

So now that we have a value in there I can explain how index's work.
Since the tables index starts at 0, as soon as you put an item in it, the index count goes up. So now, 'Hello World' has an index of 1 in the table. To call this you do:

t[1]

Here is an example of using this:

outputChatBox(t[1])
t = {position1, position2, position3, etc}
t = {index1, index2, index3, etc}


I wont cover keys because I don't really need to but I will cover loops.
When you use the function getElementsByType, it returns a table, that table contains elements. You can look up a list of elements on MTA's Wiki, I'll post a link below.
You use this function to get a table of all the elements of the specified type.

Say we get the players in the server for example:

local t = getElementsByType('player')

Each player is put into a table, and the index simply is the position inside the table where a specific player is. So say you only wanted the 4th player in the server:

local t = getElementsByType('player')
killPed(t[4])

and then we would kill that player.

#table just returns the amount of items in a table
So say we wanted to get the amount of players in the server:

local t = getElementsByType('player')
print(#t)


So now onto loops.
There are two types of loops, a pairs loop and an integer loop.
The difference between a pairs loop and an integer loop is simple. An integer loop simply goes from one number to another, basically counting up, or down if you will. A pairs loop actually runs through a table and returns its data in defined variables.
Example of an integer loop: for i=1, 5 do
this will go from 1 to 5, in like a count-up. i is your defined variable that you use to get what number it is on.

for i=1, 5 do print(i) end

so this would print:
1
2
3
4
5
An example of using this would be

local t = {'hello', 'world'}
for i=1, #t do print(t[i]) end

this would print:
hello
world

Example of a pairs loop: for i, v in pairs(getElementsByType('player')) do
Inside of the pairs function would go your table, i becomes your index and v becomes your value/item, this returns both and they will always match each-other, so here's an example of using that:

local t = {'hello', 'world'}
-- and you can use either the index method shown above or use v
for i, v in pairs(t) do print(v); print(t[i]); end

I wasn't going to go through and fix the code I give you because I honestly expected it to work. So I figured I'd teach you and other people something, in hopes that you can solve the issue yourself. I hope that this response helps you and I wish you a good day.

Page to MTA SA Element List:
Element

Edited by ShayF
  • Thanks 1
Link to post

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