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

What does the v stand for in this script?

Asked by 7 years ago
Edited 7 years ago

In the script below, what does "v" stand for?

local poweredAxles      =  script.Parent.Parent.Parent.Parent.R46ACar.MovingParts

function SetThrottle( newThrottle )
    for _,v in pairs(poweredAxles) do
        v.BodyVelocity.velocity = v.CFrame.lookVector * newThrottle * 280
    end
end

0
V is equal to the child inside newThrottle that is selected from the loop xuefei123 214 — 7y

1 answer

Log in to vote
3
Answered by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
7 years ago

In loops like these, generally i and v are used. i is the index, or key, & v is the value. Consider this table:

local tab = {
    ["Pies"] = "Delicious!",
    ["Cookies"] = "Scrumptious!",
    ["Salads"] = "..Eh.",
}

If we used an in pairs loop on this table, it would look something like this:

local tab = {
    ["Pies"] = "Delicious!",
    ["Cookies"] = "Scrumptious!",
    ["Salads"] = "..Eh.",
}

for i, v in pairs(tab) do
    print(i .." are " ..v)
end

This would print: "Pies are Delicious!", "Cookies are Scrumptious!", and "Salads are ..Eh.".

Now imagine 'tab' is 'poweredAxles'; "Delicious", "Scrumptious", and "Eh" would be the children of it.

When you get the children of an instance, it returns a table of those children. So, looping through them is the same as looping through a table. Each iteration of the loop is a different child within whatever you're getting the parent of.

So, inside of the SetThrottle function, it attempts to loop through 'poweredAxles' and set the BodyVelocity of each child (represented by v).

The script seems to have some issues, though:

local poweredAxles = script.Parent.Parent.Parent.Parent.R46ACar.MovingParts

function SetThrottle( newThrottle )
    for _,v in pairs(poweredAxles:GetChildren()) do --// You need to use :GetChildren() to get the array of children.
        v.BodyVelocity.Velocity = v.CFrame.lookVector * newThrottle * 280 --// Velocity should be capitalised.
    end
end

Hope this helped!

0
Okay, I understand now. So the "BodyVelocity" object needs to be in all of "poweredAxles" children in order for it to be set, correct? scripthelpersorg 10 — 7y
0
Yes. If the BodyVelocity object is not there, the script will error. GoldenPhysics 474 — 7y
Ad

Answer this question