Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

What does the v stand for in this script?

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

1 comment

0

V is equal to the child inside newThrottle that is selected from the loop

1 answer

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!

2 comments

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?

0

Yes. If the BodyVelocity object is not there, the script will error.

This discussion is preserved as it was. New answers, comments, and votes are closed.