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 » Scripting Helpers
Home Search
 

Re: for loop confusion

Previous Thread :: Next Thread 
Zeloi is not online. Zeloi
Joined: 27 Sep 2010
Total Posts: 1143
01 Jul 2014 01:39 PM
function StartGame()
for i,v in ipairs(Players) do
EndGame = false
v.Val.Value = 0
print(v.Val.Value)
script.Parent.Value.Value = true
v.Character:MoveTo(Workspace.BasePlate.Position + Vector3.new(math.random(-75,75),math.random(2,30),math.random(-75,75)))
game.Lighting:FindFirstChild("RocketLauncher"):Clone().Parent = v.Character
print'3'
end
end

Will only teleport one player. ._.
Report Abuse
KEVEKEV77 is not online. KEVEKEV77
Joined: 12 Mar 2009
Total Posts: 6961
01 Jul 2014 01:41 PM
function StartGame()
for i,v in ipairs(game.Players:GetChildren()) do
EndGame = false
v.Val.Value = 0
print(v.Val.Value)
script.Parent.Value.Value = true
v.Character:MoveTo(Workspace.BasePlate.Position + Vector3.new(math.random(-75,75),math.random(2,30),math.random(-75,75)))
game.Lighting:FindFirstChild("RocketLauncher"):Clone().Parent = v.Character
print'3'
end
end
Report Abuse
Zeloi is not online. Zeloi
Joined: 27 Sep 2010
Total Posts: 1143
01 Jul 2014 01:43 PM
Sorry Players is a variable i didn't show the variable is:

local Players = game.Players:GetPlayers()
Report Abuse
KEVEKEV77 is not online. KEVEKEV77
Joined: 12 Mar 2009
Total Posts: 6961
01 Jul 2014 01:44 PM
try changing ipairs to pairs?

And make sure to call teh function.
Report Abuse
DeSpizer27 is not online. DeSpizer27
Joined: 13 Jul 2012
Total Posts: 2679
01 Jul 2014 01:45 PM
Defining it as a variable will not be updated, so since the game runs when a server is started, and a server is started when a player enters, it will only be 1 player. You can use for i,v in ipairs(game.Players:GetPlayers()) do to fix this, or you could do this to the players variable;

coroutine.wrap(function()
Players = game.Players:GetPlayers()
end)()
Report Abuse
Zeloi is not online. Zeloi
Joined: 27 Sep 2010
Total Posts: 1143
01 Jul 2014 01:45 PM
It does get called but it only tp's on player.
Report Abuse
DeSpizer27 is not online. DeSpizer27
Joined: 13 Jul 2012
Total Posts: 2679
01 Jul 2014 01:46 PM
Sorry, I meant to loop it, since there's no other reason to put it in a coroutine.

coroutine.wrap(function()
while true do
Players = game.Players:GetPlayers()
end
end)()
Report Abuse
DeSpizer27 is not online. DeSpizer27
Joined: 13 Jul 2012
Total Posts: 2679
01 Jul 2014 01:47 PM
while wait() do*
Report Abuse
Zeloi is not online. Zeloi
Joined: 27 Sep 2010
Total Posts: 1143
01 Jul 2014 02:21 PM
umm i have all the code in a PlayerAdded Event.
Report Abuse
Zeloi is not online. Zeloi
Joined: 27 Sep 2010
Total Posts: 1143
01 Jul 2014 03:21 PM
Bump.
Report Abuse
Zeloi is not online. Zeloi
Joined: 27 Sep 2010
Total Posts: 1143
01 Jul 2014 03:50 PM
Bump.
Report Abuse
Zeloi is not online. Zeloi
Joined: 27 Sep 2010
Total Posts: 1143
01 Jul 2014 04:11 PM
Bump.

anyone.

please.

help.
Report Abuse
ked2000 is not online. ked2000
Joined: 10 Jul 2011
Total Posts: 1059
01 Jul 2014 04:20 PM
The variable 'Players' is only being evaluated once. So if there was only one player present at the time the script was executing, the only player that would be teleported would be the player that was present at that time. So rather than using a variable for getting a dynamic table, just access it directly 'game.Players:GetPlayers()'.
Report Abuse
Zeloi is not online. Zeloi
Joined: 27 Sep 2010
Total Posts: 1143
01 Jul 2014 04:28 PM
or could i just use a global variable instead of local?
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
01 Jul 2014 04:33 PM
Skip to section 3.2. Make sure you fully understand what you're doing before you continue.


Table Of Contents

0. Introduction
1. While
2. Repeat
3. For
-3.1 Numeric
-3.2 Generic
4. Recursion
5. Conclusion


0. Introduction

Learn everything about basic loops! Read on!

1. The while loop!

The while loop is a loop that runs on until a condition is false. It checks before every loop.

while condition do
print(true)
end

The condition is also used in if statements. Here is a condition example;

X = 5
while X == 5 do
print(true)
end

^However, that would crash. It's trying to do stuff without waiting!

X = 5
while X == 5 do
wait()
print(true)
end

To make it endless you could do this;

while true do --true is always true
wait()
print(true)
end

To shorten code, most people do this for endless loops;

while wait() do
print(true)
end

2. The repeat loop!

The repeat loop is a lot like the while loop, except unlike the while loop, it runs things UNTIL condition is true, where as the while loop runs things WHILE condition is true

repeat wait()
print(true)
until condition --repeat is the only case on which a scope does not end with "end" and it ends with "until"

To make this endless ^

repeat wait()
print(true)
until false --false is never true


3. The for loop!

3.1 Numeric

The Numeric for loop is fairly simple.

for var = startval, endval, upby do wait()
print(true)
end

Each loop "var" (which is local to the loop) will go up by "upby". When the loop starts, it will start with "startval". The loop will continue until var has reached "endval"

"upby" is optional, if you leave it out, it'll equal 1

To make this infinite;

for I = 1,math.huge() do wait()
print(true)
end

3.2 Generic

You can make your own, however most people prefer to use pairs and ipairs (or next)

Pairs and ipairs are almost the same thing, except ipairs will stop at nil values. Pairs will not.

(I)pairs is used for tables. Here is an example


Table = {"Hello", "World"}
for index, val in pairs(Table) do
print(index, val)
end

index is the current loop it is on. val is the value in the table it is on.
It automatically ends at the end if the table.

To accomplish the above using a Numeric, you could do this;

Table = {"Hello", "World"}

for I = 1,#Table do
print(I, Table[I])
end

However the above would not work for a dictionary;

Table = {Hello = "Hello", World = "World"}
for I = 1,#Table do
print(I, Table[I])
end

#Table returns 0 if it's a dictionary. To cycle through, you would use pairs;

Table = {Hello = "Hello", World = "World"}
for I,v in pairs(Table) do
print(I, v)
end

4. The recursion loop!

The recursion loop, is basically a homemade loop :D It's most commonly used for looping through every single descendent. You can accomplish this by calling a function in itself. Here's an example for looping 10 times;

function Loop(Cur, End, UpBy)
if UpBy == nil
UpBy = 1
end
print(Cur)
wait(1)
if Cur ~= End then
Loop(Cur + UpBy, End, UpBy)
end
end

Loop(1, 10, 1) --3rd argument is optional


Output;
1
2
3
4
5
6
7
8
9
10

You can easily do what I just did with a Numeric,

for I = 1,10,1 do
wait(1)
print(I)
end

But it was just an example.

5. Conclusion

There you go, that's all you need to know about loops! Have fun :D
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
01 Jul 2014 04:37 PM
And if you didn't enjoy that huge block of spam, here's the answer to your question XD


function StartGame()
local Players = game.Players:GetPlayers()
EndGame = false
v.Val.Value = 0
print(v.Val.Value)
script.Parent.Value.Value = true
for i,v in ipairs(Players) do
v.Character:MoveTo(Workspace.BasePlate.Position + Vector3.new(math.random(-75,75),math.random(2,30),math.random(-75,75)))
game.Lighting:FindFirstChild("RocketLauncher"):Clone().Parent = v.Character
print'3'
end
end
Report Abuse
Zeloi is not online. Zeloi
Joined: 27 Sep 2010
Total Posts: 1143
01 Jul 2014 06:29 PM
That will error because v is nil on line 4 & 5.
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
01 Jul 2014 06:45 PM
Sorry stick that for line below the GetPlayers line XD
Report Abuse
alij12 is not online. alij12
Joined: 03 Oct 2011
Total Posts: 1204
01 Jul 2014 06:48 PM
function StartGame()
for i,v in pairs(Players) do --why not use pairs instead of ipairs?
EndGame = false
v.Val.Value = 0
print(v.Val.Value)
script.Parent.Value.Value = true
v.Character:MoveTo(Workspace.BasePlate.Position + Vector3.new(math.random(-75,75),math.random(2,30),math.random(-75,75)))
game.Lighting:FindFirstChild("RocketLauncher"):Clone().Parent = v.Character
print'3'
end
end
Report Abuse
Zeloi is not online. Zeloi
Joined: 27 Sep 2010
Total Posts: 1143
01 Jul 2014 08:17 PM
Dont work :/
Report Abuse
Zeloi is not online. Zeloi
Joined: 27 Sep 2010
Total Posts: 1143
01 Jul 2014 10:41 PM
Bump.
Report Abuse
Notunknown99 is not online. Notunknown99
Joined: 05 Sep 2008
Total Posts: 25360
01 Jul 2014 10:52 PM
for i,v in ipairs(Players) do

Replace that line with

for i,v in next, game.Players:GetPlayers() do
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • 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