Jump to content

Table Empty issue


Recommended Posts

Hey dear community, today Im having a little issue when the player leave the server.

Well, Im currently updating the players in a table when they join the arena, leave and quit. Well, the issue is when the arena is empty, I want to set a status but it doesn't work

setRaceState function only changes the value of a variable, but it doesn't work. When I join and check the variable value with a different custom function it drops me the old one and not "none" as it should be due to the arenaPlayers table is 0.

I don't know if you have gotten me at all, but I hope you can bring me a tip to fix it.

function updatePlayersInArena()
	arenaPlayers = {}
	local players = getElementsByType("player")
	for i,player in ipairs(players) do
		if getElementData(player,"arenaTag") then
			table.insert(arenaPlayers,player)
		end
	end
	if #arenaPlayers <= 0 then
		outputChatBox("Arena is empty, setting default state")
		setRaceState("none")
	end
end
addEventHandler( "onPlayerQuit", getRootElement(), updatePlayersInArena)

Best regards.

Link to comment
  • Moderators

From what I understood, if a player is on the arena and he quits the server, his element-data "arenaTag" will still be active until the whole function is executed, and in this case, arenaPlayers wouldn't return 0 players.

Although, I'm not sure if getElementsByType function inside "onPlayerQuit" event includes the player who left the server.

Link to comment

You're doing everything wrong, my friend.

 

 if #arenaPlayers <= 0 then  outputChatBox("Arena is empty, setting default state")  setRaceState("none") end 

will never be zero

if #arenaPlayers <= 0 then
table.insert(arenaPlayers,player)

You are adding the player to the table so it will not return zero.

Edited by ZL|LuCaS
Link to comment
2 hours ago, DNL291 said:

From what I understood, if a player is on the arena and he quits the server, his element-data "arenaTag" will still be active until the whole function is executed, and in this case, arenaPlayers wouldn't return 0 players.

Although, I'm not sure if getElementsByType function inside "onPlayerQuit" event includes the player who left the server.

Thats exactly what I mean. However, i think that Im going to change the way of adding players to if not arenaPlayer [thePlayer] then

arenaPlayer [thePlayer] = true

end

And when the player stay in the arena and leaves the server 

arenaPlayer [thePlayer] = nil

If you know another way which is similar to the first one commented, just leave an answer.

2 hours ago, ZL|LuCaS said:

You're doing everything wrong, my friend.

 


 if #arenaPlayers <= 0 then  outputChatBox("Arena is empty, setting default state")  setRaceState("none") end 

will never be zero


if #arenaPlayers <= 0 then

table.insert(arenaPlayers,player)

You are adding the player to the table so it will not return zero.

Im not adding any player because the function shouldnt add any player if there is not anyone on the server. And the table.insert is before the check players in table condition.

Thanls and regards.

Link to comment
Your English does not help but I guess you do not know what you're doing.
local arenaPlayers = {}
function updatePlayersInArena() 
  --local players = getElementsByType("player") 
  for id,player in ipairs(arenaPlayers) do     
    if player == source then      
      --if getElementData(player,"arenaTag") then       
      table.remove(arenaPlayers,id)      
      --end  
    end 
  end 
  if #arenaPlayers <= 0 then  
    outputChatBox("Arena is empty, setting default state")  
    setRaceState("none") 
  end
end
addEventHandler( "onPlayerQuit", getRootElement(), updatePlayersInArena)

 

  • Like 1
Link to comment

I won't do undo my old function due to It is managed by other scripts too so I tried to implement your's one with mine one. Sadly I didn't get the solution

Look, it is still not working

function updatePlayersInArena()
	arenaPlayers = {}
	local players = getElementsByType("player")
	for i,player in ipairs(players) do
		if getElementData(player,"arenaTag") then
			table.insert(arenaPlayers,player)
		end
	end
	if #arenaPlayers < 1 or not arenaPlayers then
		outputChatBox("Arena is empty, setting default state")
		setRaceState("none")
	end
end

function playerLeavingServer()
	for id,player in ipairs(arenaPlayers) do     
		if player == source then      
		  table.remove(arenaPlayers,id)
		end 
	end 
	updatePlayersInArena()
end
addEventHandler( "onPlayerQuit", getRootElement(), playerLeavingServer)

 

Link to comment
  • Moderators

Try setting 'false' to the player's element-data:

function updatePlayersInArena()
	arenaPlayers = {}
	setElementData( source, "arenaTag", false ) 
	local players = getElementsByType("player")
	for i,player in ipairs(players) do
		if getElementData(player,"arenaTag") then
			table.insert(arenaPlayers,player)
		end
	end
	if #arenaPlayers <= 0 then
		outputChatBox("Arena is empty, setting default state")
		setRaceState("none")
	end
end
addEventHandler( "onPlayerQuit", getRootElement(), updatePlayersInArena)

 

Edited by DNL291
  • Like 1
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...