grazia

[QUESTION] How to get specific elements data from all player?

Recommended Posts

Hello, sorry this is my first thread for asking something about how to get specific elements data for all players

i've tried some of code like getElementsByType, getElementData, but still i dont know how, and im new in Lua programming, but experience in code like C, and website programming such as php, jvscript.


addCommandHandler( "check", function(player)

	

			local checkplayerid = getElementsByType("playerid",below)
			local first = 1
			local found = false
			local i =0

			while(found == false) do

				local yes = isValueInTable(checkplayerid,first,i)
				
				if yes then
					outputChatBox(first .. 'and' .. i .. 'is same',player,255,100,100)
					
				else
					outputChatBox(first .. 'is found!',player,255,100,100)
					setElementData(player,'playerid',first)
					found = true
				end
				first = first + 1
				i = i + 1

			end


end)

function isValueInTable(theTable,value,columnID)
    assert(theTable, "Bad argument 1 @ isValueInTable (table expected, got " .. type(theTable) .. ")")
    local checkIsTable = type(theTable)
    assert(checkIsTable == "table", "Invalid value type @ isValueInTable (table expected, got " .. checkIsTable .. ")")
    assert(value, "Bad argument 2 @ isValueInTable (value expected, got " .. type(value) .. ")")
    assert(columnID, "Bad argument 3 @ isValueInTable (number expected, got " .. type(columnID) .. ")")
    local checkIsID = type(columnID)
    assert(checkIsID == "number", "Invalid value type @ isValueInTable (number expected, got " ..checkIsID .. ")")
    for i,v in ipairs (theTable) do
        if v[columnID] == value then
            return true,i
        end
    end
    return false
end

 

im using isValueInTable from wiki mta for check the value in table

im just a new scripter in Lua, im sorry for ugly code.

sorry bad english

Edited by grazia
editing mistake

Share this post


Link to post
1 minute ago, WorthlessCynomys said:

There's literally a:


getAllElementData

function

 

so for example there is element "playerid" from all players online, i can get all value from getAllElementData? 

local allPlayerID = getAllElementData("playerid")

??

Share this post


Link to post

@grazia

 

local players = getElementsByType("player")

-- test code
if #players > 0 then
	setElementData(players[math.random(#players)], "something", "something")
end
--

local found = false

for i=1, #players do
	local player = players[i]
	local something = getElementData(player, "something")
	if something == "something" then
    
		iprint(getPlayerName(player), " has something...")
    
		found = true

		break -- stop searching
	end
end

 

  • Thanks 1

Share this post


Link to post
38 minutes ago, IIYAMA said:
  • 
    for i=1, #players do
    local player = players[i]
    local something = getElementData(player, "something")
    if something == "something" then
    
    iprint(getPlayerName(player), " has something...")
    
    found = true
    
    break -- stop searching
    end
    end

     

  •  

 

 

Damn, it worked! many thanks! i've learned a lot. but i just want to ask, what  is '#' for?

 

and this code you write it just like giving value to random players isn't?

-- test code
if #players > 0 then
	setElementData(players[math.random(#players)], "something", "something")
end
--

 

  • Like 1

Share this post


Link to post
54 minutes ago, grazia said:

Damn, it worked! many thanks! i've learned a lot. but i just want to ask, what  is '#' for?

 

# operator gives you back the size of the following table. In the example what @IIYAMA write to you means, that the forloop loops over all of the players one by one and if its found the value what you seach for just simple stop the looping.
 

And yes, math.random(#players) give you back a random index from the range of the players table. With the index you can get the player element from the players table and give an element data for it.

Edited by Awang
  • Like 1

Share this post


Link to post
1 hour ago, Awang said:

# operator gives you back the size of the following table. In the example what @IIYAMA write to you means, that the forloop loops over all of the players one by one and if its found the value what you seach for just simple stop the looping.
 

And yes, math.random(#players) give you back a random index from the range of the players table. With the index you can get the player element from the players table and give an element data for it.

Thank you for your kindess

  • Like 1

Share this post


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.