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: for _, var in pairs(event()) do

Previous Thread :: Next Thread 
OnlineOne is online. OnlineOne
Joined: 14 Jul 2011
Total Posts: 1193
13 Apr 2016 07:03 PM
I know some stuff about scripting, and I somewhat understand this for statement, but what on Earth is the '_,' or 'in pairs' ? Please someone explain this to me.


#code "Thanks in advance" -- Online
Report Abuse
Flux_Capacitor is not online. Flux_Capacitor
Joined: 07 Apr 2008
Total Posts: 45720
13 Apr 2016 07:04 PM
_ is just a variable name they used, and that whole
for blah in blah is a generic for loop
Report Abuse
OnlineOne is online. OnlineOne
Joined: 14 Jul 2011
Total Posts: 1193
13 Apr 2016 07:07 PM
Ah, that makes sense, thanks. And for the 'in pairs' part?


#code "Thanks in advance" -- Online
Report Abuse
SenseiWarrior is online. SenseiWarrior
Joined: 09 Apr 2011
Total Posts: 12140
13 Apr 2016 07:08 PM
its just a loop that iterates through table


local table1 = {"this","is","a","loop"}

for index, value in pairs(table1) do

print(index, value)

end


Instance.new("BodyThrust",SenseiWarrior).position = CFrame.new(SenseiWarrior,YourGirlsDMs)
Report Abuse
OnlineOne is online. OnlineOne
Joined: 14 Jul 2011
Total Posts: 1193
13 Apr 2016 07:10 PM
Ah, so it's what makes the for repeat.


#code "Thanks for the help. Case closed." -- Online
Report Abuse
Flux_Capacitor is not online. Flux_Capacitor
Joined: 07 Apr 2008
Total Posts: 45720
13 Apr 2016 07:11 PM
That's just a function that is called which returns an 'iterator' function which the generic for loop uses to actually iterate.

That sounds confusing, but maybe this'll help

http://www.lua.org/pil/4.3.5.html
http://www.lua.org/pil/7.2.html
Report Abuse
lululukas is not online. lululukas
Joined: 23 Aug 2010
Total Posts: 1043
13 Apr 2016 07:11 PM
It loops through a table

in your example the _ is the index number and the var is the variable, I personally use i,v but you literally can use whatever you want.

Put a bunch of parts and name them differently in ReplicatedStorage and use this script and you will see what it does

for i,v in pairs(game.ReplicatedStorage:GetChildren()) do
print(i,v)
end
Report Abuse
OnlineOne is online. OnlineOne
Joined: 14 Jul 2011
Total Posts: 1193
13 Apr 2016 07:13 PM
That makes sense. So in i,v the i is the number, and v is the variable.


#code "Thanks" -- Online
Report Abuse
OnlineOne is online. OnlineOne
Joined: 14 Jul 2011
Total Posts: 1193
13 Apr 2016 07:17 PM
Also, why won't this work?

local players=game:GetService("Players")
local parent=script.Parent

players:GetPlayers(function(p)
print(p.Name+" has entered game.")

parent.Text = "Players: "..tonumber(p)
end)


#code "Thanks" -- Online
Report Abuse
SenseiWarrior is online. SenseiWarrior
Joined: 09 Apr 2011
Total Posts: 12140
13 Apr 2016 07:19 PM
game.Players.PlayerAdded:connect(function(newPlayer)
print(newPlayer.Name)
end)


Instance.new("BodyThrust",SenseiWarrior).position = CFrame.new(SenseiWarrior,YourGirlsDMs)
Report Abuse
OnlineOne is online. OnlineOne
Joined: 14 Jul 2011
Total Posts: 1193
13 Apr 2016 07:27 PM
Sorry, still doesn't work.


#code "Thanks" -- Online
Report Abuse
OnlineOne is online. OnlineOne
Joined: 14 Jul 2011
Total Posts: 1193
13 Apr 2016 07:29 PM
Am I lacking a loop?

local players=game:GetService("Players")
local parent=script.Parent

while wait() do
players:GetPlayers(function(p)
print(p.Name+" has entered game.")

parent.Text="Players: "..tonumber(p)
end)
end


#code "Thanks" -- Online
Report Abuse
ausmel105 is not online. ausmel105
Joined: 05 Oct 2010
Total Posts: 251
13 Apr 2016 07:37 PM
What SenseiWarrior has given you should work.

game.Players.PlayerAdded:connect(function(newPlayer)
print(newPlayer.Name)
parent.Text = "Players: "..tostring(#(game.Players:GetChildren()))
end)
Report Abuse
lululukas is not online. lululukas
Joined: 23 Aug 2010
Total Posts: 1043
13 Apr 2016 07:40 PM
You dont need one for this

game.Players.PlayerAdded:connect(function(player)
print(player.Name.." ".."Has entered the game!")
end)

plrs=game.Players:GetPlayers()

script.Parent.Text="Players:".."..#plrs
Report Abuse
OnlineOne is online. OnlineOne
Joined: 14 Jul 2011
Total Posts: 1193
13 Apr 2016 07:41 PM
Horray! Finally got results. Thanks for the help.


#code "Thanks" -- Online
Report Abuse
lululukas is not online. lululukas
Joined: 23 Aug 2010
Total Posts: 1043
13 Apr 2016 07:41 PM
Oops a stray quote there

game.Players.PlayerAdded:connect(function(player)
print(player.Name.." ".."Has entered the game!")
end)

plrs=game.Players:GetPlayers()

script.Parent.Text="Players:"..#plrs
Report Abuse
OnlineOne is online. OnlineOne
Joined: 14 Jul 2011
Total Posts: 1193
13 Apr 2016 07:42 PM
Yup I fixed it, works great. Thanks again.


#code "Thanks" -- Online
Report Abuse
OnlineOne is online. OnlineOne
Joined: 14 Jul 2011
Total Posts: 1193
13 Apr 2016 07:46 PM
Just out of curiosity, are hashtags tonumber() things for short?


#code "Thanks" -- Online
Report Abuse
lululukas is not online. lululukas
Joined: 23 Aug 2010
Total Posts: 1043
13 Apr 2016 07:47 PM
Hashtags are used to get the number of items in the table.
Report Abuse
OnlineOne is online. OnlineOne
Joined: 14 Jul 2011
Total Posts: 1193
13 Apr 2016 08:01 PM
Ah, alright. Sorry, last question. Why won't this work?

function playerAdded()
local plrnum=game.Players:GetPlayers()

script.Parent.Text="Players:".." "..#plrnum
end

game:GetService("Players").PlayerAdded:connect(playerAdded)


#code "Thanks" -- Online
Report Abuse
lululukas is not online. lululukas
Joined: 23 Aug 2010
Total Posts: 1043
13 Apr 2016 08:02 PM
Because the function only runs when a new player is added.
Report Abuse
OnlineOne is online. OnlineOne
Joined: 14 Jul 2011
Total Posts: 1193
14 Apr 2016 02:37 PM
When you join it doesn't register you as a player added? I suppose that makes sense. Thanks for the help.


#code "Thanks" -- Online
Report Abuse
lululukas is not online. lululukas
Joined: 23 Aug 2010
Total Posts: 1043
14 Apr 2016 02:48 PM
It does, on second thought, that should probably work. Any errors?
Report Abuse
OnlineOne is online. OnlineOne
Joined: 14 Jul 2011
Total Posts: 1193
14 Apr 2016 02:49 PM
Nope, but unfortunately, this only sort of works. I just tried getting on with my mobile, and it updated the text on there, but not on my computer. Here's the code.

while wait(1) do
local plrs=game.Players:GetPlayers()

script.Parent.Text="Players:".." "..#plrs
end


#code "Thanks" -- Online
Report Abuse
MDClockwork is not online. MDClockwork
Joined: 08 Mar 2015
Total Posts: 1400
14 Apr 2016 02:53 PM
Do you have any code above that in the same script?

Sometimes if you have code above it, it will not work

such as while wait() do

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