Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Few Questions About Teleportation Script?

Asked by 5 years ago

So I got this script for wiki.roblox, and I had a few questions I'm hoping you can answer:

First of all, here is the script:

local target = CFrame.new(0 , 50, 0)

for i, player in ipairs(game.Players:GetChildren()) do
    if player.Character and
        player.Character:FindFirstChild("HumanoidRootPart") then
        player.Character.HumanoidRootPart.CFrame = target + Vector3.new(0, i * 5, 0)
    end
end

And here is the link to the website: <http://wiki.roblox.com/index.php?title=Teleporting_Within_a_Place >

So here is my first question:

  • What does the i stand for? It seems to me like it is a variable that goes up by 1 every time the loop repeats, is that correct, or is it something else?

  • What does the player stand for? Is it like a parameter that references the function that is written inside the loop? That's what I think, but I'm not certain.

  • What is the difference between ipairs and pairs? This I have no idea what they mean.

  • Finally could you say target + CFame.new(0, i * 5, 0) or do you only use the one or the other. I know that you need to use CFrame to teleport a player without killing them, but do you ever need to use Vector 3?

I hope you can help me, and know that answering these would be a lot of work, so if you can it is very much appreciated. Thank you for your time and have a nice day.

2 answers

Log in to vote
0
Answered by 5 years ago

Generic For loops are loops that iterate/loop through a table of values. pairs and ipairs are pretty similar in the way that they both return the keys and values of the table.

Table Basics

A table is a data type used to store multiple values. It is broken down into Keys and Values.

An example:

local thisTbl = {"A", "B", "C", "D"}

for key, value in pairs (thisTbl) do
    print(key, value) -- the first iteration would print 1 "A", then 2 "B", etc.
end


Keys can be understood as an address within the table. Whether it be a number or a string.

Values are the actual value within the address.

pairs vs. ipairs

ipairs will iterate through the table starting from 1 and until it reaches the last key or it reaches a nil value. ipairs can only loop through a table whose keys are integers.

pairs will iterate through the table regardless of the key's value type nor the the key's actual value. This makes it useful for looping through dictionaries.

Personally, I just stick with pairs as it can be used for both, rather ipairs is limited to integer keys.

Lastly,

Vector3's are just as important as CFrame values. There are some more advanced applications of the two and how they can be applied to one another to create interesting effects. Though, Vector3's are used to edit Size, Position or Velocity.

Vector3's will not go through other occupied points, so if you were to place two parts at the same position, one would be placed directly above it as that position was already occupied. By editing the Part's CFrame, they can be placed into one another and at the same position.

Hope I could clarify some things, feel free to ask questions! The Wiki will also contain much more detailed information.

1
ipairs can loop through dictionaries yyyyyy09 246 — 5y
0
Yes, if the value has integer keys.  PreciseLogic 271 — 5y
Ad
Log in to vote
0
Answered by
yyyyyy09 246 Moderation Voter
5 years ago
Edited 5 years ago

i is just a variable in the for loop expression

for var=exp1,exp2,exp3 do

i is just a replacement for "var", I like to think i stands for index when iterating for tables.

exp1 is where the count starts at, exp2 is where the count ends, exp3 is how much the count goes up by.

For example


for i=0,10,2 do print(i) end --[[ output: 0 2 4 6 8 10 -]]

Then the loop would end.

Player

player is just a placeholder variable in a way it would be considered as a "anonymous variable" even though behind the scenes that's not the case.

when you're iterating over a table aka ":GetChildren()" (It returns a table) its almost the same as those T tables you used to use in math class.

http://prntscr.com/jqhzt2 my table got screwed up cause of formatting ):

And so on.

So ipairs and pairs are "basically" the same, they do have some differences, but first let me explain what they do.

when you have a table of values that you want to go through, without manually getting every single item in a table you want to use either ipairs or pairs.

for example, say you want to change the color of all of the parts in a specific model to red

-- this is how you would go around doing that.

for i, v in pairs(model:GetChildren()) do

    v.Color = Color3.new(255,0,0)
    print(i, v.Name)
end

the output would be

1 part1 2 part2 3 part3 4 part4

and so on.

all it does is it gets the index aka "1,2,3,4,..." and the parts "part1,part2,part3,part4,..." and it puts the index into "i" and the value that's associated with the index into "v".

ipairs - Returns an int value, a function, and a table. ipairs iterates in order

print(ipairs({"just","table","stuff"})) this would print something like "function: 34AE123F table: AFB2345 0"

pairs - Returns a function, and a table. pairs does not iterate in order instead its more of a general iterator, meaning it'll give all the data, but not necessarily in complete order

print(pairs({"just","table","stuff"})) this would print something like "function: 34AE123F table: AFB2345 nil"

http://www.luafaq.org/#T1.10

I wouldn't be able to tell you why they use cframe - vector3 because I fully don't understand it myself with the "world space" and stuff, but here's the page where it references cframe - vector3, hope it will help you.

http://wiki.roblox.com/index.php?title=CFrame#Operators

Answer this question