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
 

Re: Is there an easier way to do this?

Previous Thread :: Next Thread 
rudolph101 is not online. rudolph101
Joined: 15 Jan 2009
Total Posts: 11873
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
thermonuklear is not online. thermonuklear
Joined: 19 Oct 2014
Total Posts: 484
12 Jan 2015 12:32 PM
local players = game.Players:GetChildren()

?
Report Abuse
cody123454321 is not online. cody123454321
Joined: 21 Nov 2009
Total Posts: 5408
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
cody123454321 is not online. cody123454321
Joined: 21 Nov 2009
Total Posts: 5408
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
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
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
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
12 Jan 2015 01:00 PM
Sorry @cody, I felt like I had to 1 up you :D
Report Abuse
cody123454321 is not online. cody123454321
Joined: 21 Nov 2009
Total Posts: 5408
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
cody123454321 is not online. cody123454321
Joined: 21 Nov 2009
Total Posts: 5408
12 Jan 2015 01:12 PM
Plus it is a lot of overkill.
Report Abuse
robocu3 is not online. robocu3
Joined: 13 Mar 2009
Total Posts: 6485
12 Jan 2015 01:14 PM
hardly flexible either lol
-=Robo=-
Report Abuse
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
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 is not online. 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 is not online. 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
cody123454321 is not online. cody123454321
Joined: 21 Nov 2009
Total Posts: 5408
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 is not online. 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 is not online. 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
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
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 is not online. 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
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
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
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
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
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