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