grazia Posted September 25, 2019 Share Posted September 25, 2019 (edited) 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 September 25, 2019 by grazia editing mistake Link to comment
WorthlessCynomys Posted September 25, 2019 Share Posted September 25, 2019 There's literally a: getAllElementData function Link to comment
grazia Posted September 25, 2019 Author Share Posted September 25, 2019 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") ?? Link to comment
Moderators IIYAMA Posted September 25, 2019 Moderators Share Posted September 25, 2019 @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 1 Link to comment
grazia Posted September 25, 2019 Author Share Posted September 25, 2019 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 -- 1 Link to comment
Awang Posted September 25, 2019 Share Posted September 25, 2019 (edited) 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 September 25, 2019 by Awang 1 Link to comment
grazia Posted September 25, 2019 Author Share Posted September 25, 2019 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 1 Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now