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: Generic for loop?

Previous Thread :: Next Thread 
greedyawesomeman is not online. greedyawesomeman
Joined: 18 Apr 2010
Total Posts: 1038
15 Jul 2015 07:34 PM
Is this what I would use if I wanted to find all of the lights in my game and turn them on? If not what would I use?
Report Abuse
greedyawesomeman is not online. greedyawesomeman
Joined: 18 Apr 2010
Total Posts: 1038
15 Jul 2015 08:47 PM
bump
Report Abuse
TimeTicks is not online. TimeTicks
Joined: 27 Apr 2011
Total Posts: 27115
15 Jul 2015 08:48 PM
for i,v in next, workspace:GetChildren() do
if v:IsA("PointLight") then
v.Enabled = true
end
end


"Talk is cheap. Show me the code." - Linus Torvalds
Report Abuse
lordrambo is not online. lordrambo
Joined: 16 Jun 2009
Total Posts: 20628
15 Jul 2015 08:49 PM
Well, if all the lights are in the same model or something, yeah that'd be fine. Otherwise you'd need a recursive function to search through the entire game.
Report Abuse
iSpecializinq is not online. iSpecializinq
Joined: 31 May 2015
Total Posts: 2182
15 Jul 2015 08:50 PM
for i = 1,#200 do -- change 200 to the num of lights
ypcall(function()game:FindFirstChild("LightName",true).Enabled=true;end)
end
Report Abuse
greedyawesomeman is not online. greedyawesomeman
Joined: 18 Apr 2010
Total Posts: 1038
15 Jul 2015 10:00 PM
Can someone explain it? I would like to understand what the I and V mean etc
Report Abuse
CUBES4LIFE is not online. CUBES4LIFE
Joined: 22 Jun 2009
Total Posts: 4956
15 Jul 2015 10:02 PM
I will gladly explain to someone who actually shows interest in learning. I wish we had more people like you around here! Let me write up some code with comments. I'll be back in a few minutes.
Report Abuse
TimeTicks is not online. TimeTicks
Joined: 27 Apr 2011
Total Posts: 27115
15 Jul 2015 10:06 PM
i (index) will find the number of objects. v (variable) will set the variable to those objects.

if you have a model with 4 bricks then the i (index) would = 4. The v (variable) can be used to manipulate the ojects

for i,v in next, model:GetChildren() do
print(i)
print(v.Name)
end

Output:
1
Part
2
Part
3
Part
4
Part


"Talk is cheap. Show me the code." - Linus Torvalds
Report Abuse
CUBES4LIFE is not online. CUBES4LIFE
Joined: 22 Jun 2009
Total Posts: 4956
15 Jul 2015 10:08 PM
local count = 0 -- Count is the variable counting how many players there are

for _,Player in next, game.Players:GetChildren() do -- This runs a loop as many times as there are children in players
count = count + 1 -- Every time this loop is run, count becomes + 1 higher
wait() -- always a good idea to put waits in loops
end

print(count) -- Prints only after the loop above has run, thus printing the number of players

This is one example of a generic for loop. You brought up a for loop that uses for i,v in pairs do, which is the same exact thing. In the example I provided you, I use for _,Players in next, do but I could have used for i,v in next, and received the exact same information. They are simply variables. To make myself clear, look at the following two examples. They are exactly the same, the only difference being how I word my variables.

for i,v in pairs(game.Workspace) do
v:Destroy()
wait()
end

for _,Obj in pairs(game.Workspace) do
Obj:Destroy()
wait()
end

Those two generic for loops do the same thing, the difference? The way I word my variables. You can name your variables anything, but its suggested that you use the status quo variables.

For more reference, check out Roblox's wiki on generic fors: http://wiki.roblox.com/index.php?title=Generic_for

If you have any questions, don't feel any hesitation in asking. The best way to learn is to ask questions.
Report Abuse
greedyawesomeman is not online. greedyawesomeman
Joined: 18 Apr 2010
Total Posts: 1038
16 Jul 2015 11:16 AM
Thanks guys! I think I'm starting to get it.

@Cubes4Life would you mind if I PM'd you if I had a question?
Report Abuse
epicbreaker is not online. epicbreaker
Joined: 23 Apr 2011
Total Posts: 2791
16 Jul 2015 11:19 AM
http://wiki.roblox.com/index.php?title=Generic_for
Report Abuse
redlo43 is not online. redlo43
Joined: 16 Feb 2011
Total Posts: 4722
16 Jul 2015 11:25 AM
regular fors are more efficient in your case
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