Jump to content

why is this table empty


lgeri000

Recommended Posts

Hello, can anybody tell me why is this ids table stay empty?

local maxPlayers = getMaxPlayers()
local ids = {}

addEventHandler("onPlayerJoin", root,
	function()
		local player = source
		for i = 1, maxPlayers do
			if (ids[i] == false) then
				ids[i] = i
				setElementData(player,"id",i)
				break
			end
		end
	end
)
addEventHandler("onPlayerQuit", root,
	function()
		local i = getElementData(source,"id")
		ids[i] = false
		setElementData(source,"id",false)
	end
)

this would  be an id-system

  • Confused 1
Link to post
  • Moderators
3 hours ago, lgeri000 said:

Hello, can anybody tell me why is this ids table stay empty?


local maxPlayers = getMaxPlayers()
local ids = {}

addEventHandler("onPlayerJoin", root,
	function()
		local player = source
		for i = 1, maxPlayers do
			if (ids[i] == false) then
				ids[i] = i
				setElementData(player,"id",i)
				break
			end
		end
	end
)
addEventHandler("onPlayerQuit", root,
	function()
		local i = getElementData(source,"id")
		ids[i] = false
		setElementData(source,"id",false)
	end
)

this would  be an id-system

use nil instead of false.

false = is still something, even if it is not considered as positive value.

nil = nothing

false == nil -- false (unlike in languages like JS, where that is true because the === operator is not used.)

 

Also increment the ID value, instead of doing it based on maxPlayers. Keep it simple.

local ID = 0

--onPlayerJoin
ID = ID + 1
setElementData(player,"id",ID)
--

 

 

 

 

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.

×
×
  • Create New...