|
| 12 Jan 2015 12:30 PM |
In a script, I keep wanting to access all the players by doing this:
for i, v in pairs(game.Players:GetChildren()) do blablabla end
Instead of having to say "for i, v in pairs" every time is there a way I can do it just once and access the players that way with a single line?
|
|
|
| Report Abuse |
|
|
|
| 12 Jan 2015 12:32 PM |
local players = game.Players:GetChildren()
? |
|
|
| Report Abuse |
|
|
|
| 12 Jan 2015 12:33 PM |
local doToPlayers = function(func) local t = game.Players:GetPlayers() for i, v in pairs(t)do func(t, i, v) end end
-- Kick all players doToPlayers(function(t, i, v) v:Kick() end)
|
|
|
| Report Abuse |
|
|
|
| 12 Jan 2015 12:36 PM |
local doToAll = function(t, func) for i, v in pairs(t)do func(t, i, v) end end
-- print everything from table doToPlayers({"Hello", "world"}, function(t, i, v) print("Inside of table", t, "On index", i, "Lays", v) end)
>> Inside of table TABLE0x19203ja On index 1 Lays Hello >> Inside of table TABLE0x19203ja On index 2 Lays world |
|
|
| Report Abuse |
|
|
|
| 12 Jan 2015 01:00 PM |
function UpdatePlayers() Players = game.Players:GetPlayers()
setmetatable(Players, { __call = function(func) for i, v in pairs(Players) do func(v) end end, __index = error("Attempt to index a nil value", 2), __newindex = error("Attempt to add new value", 2), __metatable = error("Protected") }) end
game.Players.PlayerAdded:connect(UpdatePlayers) game.Players.PlayerRemoving:connect(UpdatePlayers)
--Example Usage:
Players(function(p) print(p.Name) end)
--Bonus:
print(Players.warspyking.Name) --You can also use it as the game.Players object. |
|
|
| Report Abuse |
|
|
|
| 12 Jan 2015 01:00 PM |
| Sorry @cody, I felt like I had to 1 up you :D |
|
|
| Report Abuse |
|
|
|
| 12 Jan 2015 01:11 PM |
You didn't though.
You created pseudo-code that seperates the point of the @OP's needs, plus doesn't serve the @OP's topic.
|
|
|
| Report Abuse |
|
|
|
| 12 Jan 2015 01:12 PM |
| Plus it is a lot of overkill. |
|
|
| Report Abuse |
|
|
robocu3
|
  |
| Joined: 13 Mar 2009 |
| Total Posts: 6485 |
|
|
| 12 Jan 2015 01:14 PM |
hardly flexible either lol -=Robo=- |
|
|
| Report Abuse |
|
|
|
| 12 Jan 2015 01:14 PM |
| It's not overkill, it's metatables. And his method actually works quite well (Or looks like it would) |
|
|
| Report Abuse |
|
|
robocu3
|
  |
| Joined: 13 Mar 2009 |
| Total Posts: 6485 |
|
|
| 12 Jan 2015 01:17 PM |
Actually, I'm pretty sure it would error after reading the metastable page. Unless the table parameter is omitted and I'm misinterpreting what he did, he's calling a table as a function(impossible considering func isn't defined as a metatable). Could someone explain this to me? Am I misreading it? Always been bad with OOP. :P -=Robo=- |
|
|
| Report Abuse |
|
|
robocu3
|
  |
| Joined: 13 Mar 2009 |
| Total Posts: 6485 |
|
|
| 12 Jan 2015 01:17 PM |
ignore the typo, I'm on my school computer and it has an autocorrect. futuristiccc. -=Robo=- |
|
|
| Report Abuse |
|
|
|
| 12 Jan 2015 01:21 PM |
Autocorrect on a laptop or whatever? That's *curseword*
@GuyWhoDidn'tKnowAboutMetatablesAndThoughtItWasOOP
Metatables are not OOP.
local metatable = { __call = function() print("You have called this table") end; __add = function(self, i) return table[1] + i end }
local tab = {50} setmetatable(tab, metatable)
tab() print(tab + 5)
>> You have called this table >> 55
Run this script. This explains metatables. |
|
|
| Report Abuse |
|
|
robocu3
|
  |
| Joined: 13 Mar 2009 |
| Total Posts: 6485 |
|
|
| 12 Jan 2015 01:24 PM |
metatables are practically roblox's version of classes, I was given the impression they're only useful for OOP, so I just associate metastables with that. an honest mistake, especially considering I hardly use them myself. >.> And I can't run it right now, these school computers have a proxy that blocks studio. :P
-=Robo=- |
|
|
| Report Abuse |
|
|
robocu3
|
  |
| Joined: 13 Mar 2009 |
| Total Posts: 6485 |
|
|
| 12 Jan 2015 01:24 PM |
eheh *lua's version of classes o gee i'm making myself out to be an idiot aharhar -=Robo=- |
|
|
| Report Abuse |
|
|
|
| 12 Jan 2015 01:30 PM |
| Why would you call it overkill? It simply allows calling a function on all the players by calling a table of players has a function. |
|
|
| Report Abuse |
|
|
robocu3
|
  |
| Joined: 13 Mar 2009 |
| Total Posts: 6485 |
|
|
| 12 Jan 2015 01:34 PM |
@warspy first parameter supplied to the call function should be a table, wouldn't it error because you're calling func(passed as a table) onto the player? xD -=Robo=- |
|
|
| Report Abuse |
|
|
|
| 12 Jan 2015 01:40 PM |
| @robo Hmmm, I don't think so, although I do not use it often. Lemme check right which (as far as ik, the arguments to __call are the arguments that are used when you call it) |
|
|
| Report Abuse |
|
|
|
| 12 Jan 2015 01:43 PM |
My bad, you are correct. Good job picking up on that :D
function UpdatePlayers() Players = game.Players:GetPlayers()
setmetatable(Players, { __call = function(tab, func) for i, v in pairs(Players) do func(v) end end, __index = error("Attempt to index a nil value", 2), __newindex = error("Attempt to add new value", 2), __metatable = error("Protected") }) end
game.Players.PlayerAdded:connect(UpdatePlayers) game.Players.PlayerRemoving:connect(UpdatePlayers)
--Example Usage:
Players(function(p) print(p.Name) end)
--Bonus:
print(Players.warspyking.Name) --You can also use it as the game.Players object. |
|
|
| Report Abuse |
|
|