Jump to content

Help!


Hugos

Recommended Posts

I have such a system for oral ID to the player:

		function getIDFromPlayer(player)
			if player then
				local theid
				players = getElementsByType("player")
				for id,p in ipairs(players) do
					if player == p then
						theid = i
				end
			end
			return theid
		else return false end
		end

Please help. I need to make sure that every player is assigned an ID at spawn. But I now work a little off-line (when the player comes out with id 1, the player with id 2, assigned id 1, but I need this player to have id 2 left, and the one who comes in - established id 1-n  (is not busy) ).

Link to comment
  • Moderators
4 minutes ago, Hugos said:

I have such a system for oral ID to the player:

The player list is dynamic. The location of the players in the table is changing when players are leaving.

 

Player list (table)

local players = getElementsByType("player")

 

New players are added on the back of the table.

players = {
 [1] = player1, [2] = player2, [3] = player3 
}

players[4] = player4
players = {
 [1] = player1, [2] = player2, [3] = player3, [4] = player4
}

 

 

Leaving players are collapsing the list.

players = {
 [1] = player1, [2] = player2, [3] = player3, [4] = player4
}

table.remove(players, 1) -- player1 is leaving
players = {
 [1] = player2, [2] = player3, [3] = player4
}

 


 

If you want to generate IDs, you have to make sure they are always unique.

do -- < Closed environment for the variable: id. Nothing can edit it, except for the newId function.
	local id = 0
	function newId()
		id = id + 1
		return id
	end
end

local playerIDs = {}

function getIDFromPlayer(player)
	local id = playerIDs[player]
	if not id then
		id = newId()
		playerIDs[player] = id
	end
	return id
end

Note, this table doesn't clean it self, yet.

 

Link to comment
21 hours ago, IIYAMA said:

The player list is dynamic. The location of the players in the table is changing when players are leaving.

 

Player list (table)


local players = getElementsByType("player")

 

New players are added on the back of the table.


players = {
 [1] = player1, [2] = player2, [3] = player3 
}

players[4] = player4

players = {
 [1] = player1, [2] = player2, [3] = player3, [4] = player4
}

 

 

Leaving players are collapsing the list.


players = {
 [1] = player1, [2] = player2, [3] = player3, [4] = player4
}

table.remove(players, 1) -- player1 is leaving

players = {
 [1] = player2, [2] = player3, [3] = player4
}

 


 

If you want to generate IDs, you have to make sure they are always unique.


do -- < Closed environment for the variable: id. Nothing can edit it, except for the newId function.
	local id = 0
	function newId()
		id = id + 1
		return id
	end
end

local playerIDs = {}

function getIDFromPlayer(player)
	local id = playerIDs[player]
	if not id then
		id = newId()
		playerIDs[player] = id
	end
	return id
end

Note, this table doesn't clean it self, yet.

 

I didn 't get it a little bit. Then how should the function fully look?

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