JuniorMelo Posted March 7, 2016 Share Posted March 7, 2016 hello, I wanted to know if I can use this function this way ? getPlayerCount, dimension 2 ====count players in dimension 2==== addEvent("text", true) addEventHandler("text", getRootElement(), function() for k,v in ipairs(getElementsByType("player")) do if getElementDimension ( v ) == 2 then local Count = getPlayerCount(v) setElementData(source,"players",Count) end end end) Link to comment
ozulus Posted March 7, 2016 Share Posted March 7, 2016 Add a variable for player count. Like that, addEvent("text", true) addEventHandler("text", getRootElement(), function() local playerCount = getPlayerCountInDimension( 2 ) if playerCount then setElementData(source,"players",Count) end end) function getPlayerCountInDimension( dimID ) local counter = 0 for k,v in ipairs(getElementsByType("player")) do if getElementDimension ( v ) == dimID then counter = counter + 1 end end return counter end Link to comment
Moderators Citizen Posted March 7, 2016 Moderators Share Posted March 7, 2016 I think you wanted to do more like this: addEvent("text", true) addEventHandler("text", getRootElement(), function() local count = 0 for k, v in ipairs(getElementsByType("player")) do if getElementDimension ( v ) == 2 then count = count + 1 end end setElementData(source, "players", count) end) Or as a function extension: local _getPlayerCount = getPlayerCount function getPlayerCount( dim ) if dim == nil then return _getPlayerCount() end local count = 0 for k, p in ipairs(getElementsByType("player")) do if getElementDimension(p) == dim then count = count + 1 end end end That you will then call like this: addEvent("text", true) addEventHandler("text", getRootElement(), function() local count = getPlayerCount(2) setElementData(source, "players", count) end) But your code doesn't make sense as it looks like you want to output a message to all players in a specific dimension ? In that case, getting the numbers of players in that dimension won't help you. You will need the list of the players in that dimension instead: function getPlayersInDimension( dim ) local players = {} for k, p in ipairs(getElementsByType("player")) do if getElementDimension(p) == dim then table.insert(players, p) end end return players end And then use it like this: addEvent("text", true) addEventHandler("text", getRootElement(), function( msg ) local players = getPlayersInDimension(2) for k, p in ipairs(players) do outputChatBox(p, msg) end end) Don't forget to send the text message as argument of your triggerServerEvent. More informations on what you try to achieve would be appreciated. Best regards, Citizen Link to comment
JuniorMelo Posted March 8, 2016 Author Share Posted March 8, 2016 very much to everyone, it helped greatly Citizen: very well explained 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