Jump to content

Help with function "countPlayersInRange"


Odonton

Recommended Posts

Hoping that they are well, it turns out that I have a problem with my script, I want to make it work counting the players in an area and not the server to start the game. It gives me the global attempt to call countPlayersInRange (a nil value).

 

function chekin( player )--CHECK INICIO
   
    local alive =  countPlayersInRange(4754.6508789063, -2038.6687011719, 2.3671875, 40)
      
    if alive >= 8 and royale == false then -- mudar para 3
    outputChatBox ( "#FFFFFF[ROYALE]:#FF0000INICIANDO PARTIDA BATTLE ROYALE", root, 0, 255, 0, true )
    counter()
    elseif alive == 1 and royale == false then
    outputChatBox ("#FFFFFF[ROYALE]:#FF0000FALTA 7 JUGADORES", root, 255, 0, 0, true)
	elseif alive == 2 and royale == false then
    outputChatBox ("#FFFFFF[ROYALE]:#FF0000FALTA 6 JUGADORES", root, 255, 0, 0, true)
	elseif alive == 3 and royale == false then
    outputChatBox ("#FFFFFF[ROYALE]:#FF0000FALTA 5 JUGADOR", root, 255, 0, 0, true)
	elseif alive == 4 and royale == false then
    outputChatBox ("#FFFFFF[ROYALE]:#FF0000FALTA 4 JUGADORES", root, 255, 0, 0, true)
	elseif alive == 5 and royale == false then
    outputChatBox ("#FFFFFF[ROYALE]:#FF0000FALTA 3 JUGADOR", root, 255, 0, 0, true)
	elseif alive == 6 and royale == false then
    outputChatBox ("#FFFFFF[ROYALE]:#FF0000FALTA 2 JUGADORES", root, 255, 0, 0, true)
	elseif alive == 7 and royale == false then
    outputChatBox ("#FFFFFF[ROYALE]:#FF0000FALTA 1 JUGADOR", root, 255, 0, 0, true)
	end
	
end     

If you could explain why this error occurs and how to solve it would be very helpful, thanks!

Link to comment
1 minute ago, Randomly said:

Well you probably found it under the useful functions tab, so if you copy the function into your script then it'll work.

Otherwise, you can count players in a range with a colshape and a count (#)

I think it solved it by adding a content of an earlier window called "Function source", now it gives me the error that I can not compare it with a number because it is a boolean,: c, sorry for disturbing you

Link to comment
2 minutes ago, Randomly said:

function countPlayersInRange(x,y,z, range)
  local tempCol = createColSphere(x,y,z,range)
  players = getElementsWithinColShape(tempCol, "player")
  destroyElement(tempCol)
  return #players
end

 

WOW! Really thank you very much! are you the maximum, is there any way to give points or reputation in this forum? because you really deserve it

Link to comment
1 hour ago, Randomly said:

function countPlayersInRange(x,y,z, range)
  local tempCol = createColSphere(x,y,z,range)
  players = getElementsWithinColShape(tempCol, "player")
  destroyElement(tempCol)
  return #players
end

 

The last help I ask: c, it turns out that when using the same code for the players counter in the area, trying to show it as text in my HUD always shows the number 0 even though I am inside the area, with the code above if I showed all the players on the server. If you are too busy to respond, you will understand, apologize for the inconvenience.

The dxDrawText is in the line 29

 

function countPlayersInRange(x,y,z, range)
  local tempCol = createColSphere(x,y,z,range)
  players = getElementsWithinColShape(tempCol, "player")
  destroyElement(tempCol)
  return #players
end

function debugMonitor()
	if (showDebug or false) == true and (getElementData(localPlayer, "logedin") or false) == true then
	    local debugmonitor = getElementData(getLocalPlayer(), "confdebug")
		local espera = getElementData(getLocalPlayer(), "aguardando")
		local perdeu = getElementData(getLocalPlayer(), "perdeucombate")
		local combate = getElementData(getLocalPlayer(), "combate")
		local ganhou = getElementData(getLocalPlayer(), "venceu")
		local vida = getElementData(getLocalPlayer(), "blood")
		local HpLine = 0.1960* (vida / 5000)

		local count = countPlayersInRange(4754.6508789063, -2038.6687011719, 2.3671875, 40)
		tostring(getPedTotalAmmo(localPlayer) - getPedAmmoInClip(localPlayer))
		
		
		local screenW, screenH = guiGetScreenSize()
		
		dxDrawRectangle(screenW * 0.4007, screenH * 0.9635, screenW * 0.1960, screenH * 0.0264, tocolor(0,0,0, 155), false)
	    dxDrawRectangle(screenW * 0.4007, screenH * 0.9635, screenW * HpLine, screenH * 0.0264, tocolor(255,255,255, 255), false)
	    dxDrawRectangle(screenW * 0.9199, screenH * 0.0130, screenW * 0.0257, screenH * 0.0456, tocolor(0,0,0, 190), false)
	    dxDrawRectangle(screenW * 0.9456, screenH * 0.0130, screenW * 0.0457, screenH * 0.0456, tocolor(255,255,255, 190), false)
	    dxDrawText("ALIVE", screenW * 1.93, screenH * 0.02, screenW * 0.007, screenH * 0.05, tocolor(9,9,9, 150), 1.8, "default_bold", "center", "center")
	    dxDrawText(""..count, screenW * 1.858, screenH * 0.015, screenW * 0.007, screenH * 0.05, tocolor(255,255,255, 255), 2, "default_bold", "center", "top")

      
        
               
		
	    end
		
end

 

Link to comment
1 hour ago, Odonton said:

WOW! Really thank you very much! are you the maximum, is there any way to give points or reputation in this forum? because you really deserve it

It gives reputation points based on likes that comments have.

8 minutes ago, Odonton said:

The last help I ask: c, it turns out that when using the same code for the players counter in the area, trying to show it as text in my HUD always shows the number 0 even though I am inside the area, with the code above if I showed all the players on the server. If you are too busy to respond, you will understand, apologize for the inconvenience.

The dxDrawText is in the line 29

 


function countPlayersInRange(x,y,z, range)
  local tempCol = createColSphere(x,y,z,range)
  players = getElementsWithinColShape(tempCol, "player")
  destroyElement(tempCol)
  return #players
end

function debugMonitor()
	if (showDebug or false) == true and (getElementData(localPlayer, "logedin") or false) == true then
	    local debugmonitor = getElementData(getLocalPlayer(), "confdebug")
		local espera = getElementData(getLocalPlayer(), "aguardando")
		local perdeu = getElementData(getLocalPlayer(), "perdeucombate")
		local combate = getElementData(getLocalPlayer(), "combate")
		local ganhou = getElementData(getLocalPlayer(), "venceu")
		local vida = getElementData(getLocalPlayer(), "blood")
		local HpLine = 0.1960* (vida / 5000)

		local count = countPlayersInRange(4754.6508789063, -2038.6687011719, 2.3671875, 40)
		tostring(getPedTotalAmmo(localPlayer) - getPedAmmoInClip(localPlayer))
		
		
		local screenW, screenH = guiGetScreenSize()
		
		dxDrawRectangle(screenW * 0.4007, screenH * 0.9635, screenW * 0.1960, screenH * 0.0264, tocolor(0,0,0, 155), false)
	    dxDrawRectangle(screenW * 0.4007, screenH * 0.9635, screenW * HpLine, screenH * 0.0264, tocolor(255,255,255, 255), false)
	    dxDrawRectangle(screenW * 0.9199, screenH * 0.0130, screenW * 0.0257, screenH * 0.0456, tocolor(0,0,0, 190), false)
	    dxDrawRectangle(screenW * 0.9456, screenH * 0.0130, screenW * 0.0457, screenH * 0.0456, tocolor(255,255,255, 190), false)
	    dxDrawText("ALIVE", screenW * 1.93, screenH * 0.02, screenW * 0.007, screenH * 0.05, tocolor(9,9,9, 150), 1.8, "default_bold", "center", "center")
	    dxDrawText(""..count, screenW * 1.858, screenH * 0.015, screenW * 0.007, screenH * 0.05, tocolor(255,255,255, 255), 2, "default_bold", "center", "top")

      
        
               
		
	    end
		
end

 

I haven't tested the function but it should work. Try to debug it. Output the elements of the player table with a for loop. Or maybe you can not "return #players" so you have to do sg like this

local count = #players
return count

 

  • Thanks 1
Link to comment
14 hours ago, Randomly said:

Feel free to ask about this.

I really do not quite understand what I should do, when you refer to "Output the elements of the player with a for loop", apologize, I feel like an idiot asking things like this: /. By the way, we are doing a server type pubg c: if you want to tell us that such is sending you the ip by Internal? (there is an internal chat here, right?)

Link to comment
14 hours ago, Odonton said:

I really do not quite understand what I should do, when you refer to "Output the elements of the player with a for loop", apologize, I feel like an idiot asking things like this: /. By the way, we are doing a server type pubg c: if you want to tell us that such is sending you the ip by Internal? (there is an internal chat here, right?)

function countPlayersInRange(x,y,z, range)
  local tempCol = createColSphere(x,y,z,range)
  players = getElementsWithinColShape(tempCol, "player")
  destroyElement(tempCol)
local numberof = 0
  for k,v in ipairs(players) do
numberof = numberof+1
end
return numberof
end

 

  • Thanks 1
Link to comment
15 hours ago, Randomly said:

For PUBG i would use either dimensions or more servers. Not a players in range. You can count players in dimensions too.

I'll make my function on PC if you still need it and send the working one.

Wow, thanks, You are amazing, really thank you very much for everything, but I would feel badly demanding that you make me your codes. What if, now I have another problem, it results that we mapped a beautiful lobby, with landing strip, a crashed plane and so, but it works once, then disappears and I have to restart the resource "map" so that it is functional again, there some command to not restart, something like to fix the GM? :c

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