generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

dictionary of players...

Previous Thread :: Next Thread 
bigboy77584 is online. bigboy77584
Joined: 06 Jan 2009
Total Posts: 1003
30 Jul 2017 04:42 PM
I have a dictionary of players (a table for each player contained inside one big table). Based on the values inside each player, I need to be able to manipulate the player. The problem is, the method I have been trying to use doesn't work. For example

local playerList = {}
-- player joins
playerList[plr] = {value1 = false; value2 = true; value3 = true}

-- i need to be able to do this
if blahh then
playerList[plr].Character:MoveTo(someposition)
end

instead I get playerList[plr].Character == nil since playerList[plr] is just a table inside playerList. Can someone help me get around this?



Report Abuse
ExtremeBuilder15 is online. ExtremeBuilder15
Joined: 01 May 2012
Total Posts: 3176
30 Jul 2017 04:43 PM
plr.Character

Report Abuse
bigboy77584 is online. bigboy77584
Joined: 06 Jan 2009
Total Posts: 1003
30 Jul 2017 04:48 PM
Perhaps I should be more specific.... The big playerList table gets sorted into a few smaller tables and instead of having direct access to the player, it simply becomes a member of a list of tables with like values. For example

local likeValues = {}
for i, player in pairs(playerList) do
if player.value1 and not player.value2 then
table.insert(likeValues, player)
end
end

likeValues[1].Character == nil


Report Abuse
Scriptos is not online. Scriptos
Joined: 17 Jun 2008
Total Posts: 2900
30 Jul 2017 04:53 PM
This is just a very quick code that I just wrote, so it may not be the best, but you can improve on it.

It's just an example.

local playerList = {};

function addPlayerInfo(list, player, info)

local metatable = {
__index = function(tabl, index)
return player[index];
end};

setmetatable(info, metatable);
list[player] = info;
end;

game.Players.PlayerAdded:connect(function(player)

addPlayerInfo(playerList, player, {
value1 = false;
value2 = true;
value3 = true;
});

repeat wait(1/44) until player.Character;
print(playerList[player].Character);
print(playerList[player].value1, playerList[player].value2);
print(playerList[player].CameraMaxZoomDistance);
end)
Report Abuse
bigboy77584 is online. bigboy77584
Joined: 06 Jan 2009
Total Posts: 1003
30 Jul 2017 04:58 PM
ahh thanks! I had a feeling I would need metatables at some point. Maybe this will be a good time to study them a little closer.


Report Abuse
bigboy77584 is online. bigboy77584
Joined: 06 Jan 2009
Total Posts: 1003
30 Jul 2017 05:03 PM
Just a question about what you wrote. So if I were to do something like

print(playerList[player].Character)

it is checking to see if 'Character' is a value inside of info, and when it finds out that it is not, how does it know to check game.Players.Player.Character?


Report Abuse
Scriptos is not online. Scriptos
Joined: 17 Jun 2008
Total Posts: 2900
30 Jul 2017 05:06 PM
The function inside of __index fires whenever you refer to something in the table that's nil. I basically told it to check the player for that property, if it's ever nil.
Report Abuse
Scriptos is not online. Scriptos
Joined: 17 Jun 2008
Total Posts: 2900
30 Jul 2017 05:07 PM
So, if you decide to do something like;

print(playerList[player].Potato); it'll check the table for 'Potato' first, and if it doesn't exist, it checks the Player. If it doesn't exist there either, you'll get an error, so you'll have to handle that if it's going to be a problem for some reason.
Report Abuse
bigboy77584 is online. bigboy77584
Joined: 06 Jan 2009
Total Posts: 1003
30 Jul 2017 05:20 PM
One more thing, in this line

__index = function(tabl, index)

what is tabl? I have also seen it called 'self' but I am not sure what it means...


Report Abuse
Scriptos is not online. Scriptos
Joined: 17 Jun 2008
Total Posts: 2900
30 Jul 2017 05:24 PM
http://wiki.roblox.com/index.php?title=Metatable#Metamethods

You can call it whatever you want. It's probably better to just call it self, to be honest. I just have a habit of typing "table" and then realizing that I can't use that, so I just quickly remove the e. It just refers to the table that the metatable is attached to.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image