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

Need help with this transparency script plz help? Using in pairs loop

the parts are in a folder. trying to make parts go half transparent one by one

for i, v in pairs(game.Workspace.Folder:GetChildren()) do
	for i = 0, 1 -0.05 do
		v.Transparency = i
		if v.Transparency == 0.5 then
			break
		end
	end
	wait(1)
end

Also can Anyone tell me a couple of ways to learn RBLX Lua.

And if youtube then Can you suggest good (Roblox) lua Teachers

2 answers

✓ Accepted answer

I believe you have accidently reversed it. You can't get to 1 from 0, by taking off 0.05

for i, v in pairs(game.Workspace.Folder:GetChildren()) do
    for i = 1, .5 -0.05 do
        v.Transparency = i
    end
    wait(1)
end

From my experience, I would recommend having the iterated For loop outside of the Generic For, and not vise-versa.

With how you set up your code, it, in a nutshell, is doing the following:

-- Iterate through the table
-- Begin iterated for
-- Do transparency for current object
-- Do the next object after

And not:

-- Iterate through the table
-- Iterate through the table
-- Begin iterated for
-- Begin iterated for
-- Do transparency for current object
-- Do transparency for current object
-- Finish
-- Finish
-- etc...

I would also recommend using the IsA function, as a just-in-case if there's an object that isn't a part in the model object; what it does is is it checks to see if a object(s)'s ClassName, or type, is that specific object type.

With all that in mind, lets go on ahead & work on the code. :P

First off, we want to plug in the Iterated For Loop first before plugging in the Generic For:

local ForIterationValues = {
	StartValue = 0, -- Where the for loop will first begin
	EndValue = 1, -- Where to end
	Increasement = 0.1 -- How much the value will increase
} -- This isn't necessary, but I wanted to show a visual understand of how the iterated for loop works.

for i = ForIterationValues.StartValue, ForIterationValues.EndValue, ForIterationValues.Increasement do
	-- "i" is defined as the current iterated number (as it goes down/up)
end

Second, we can now plug in the Generic For; however, DO NOT put it outside of the Iterated For Loop if you want to accomplish what you're (wanting it to) doing, as vise-versa will not result in what you're trying to do:

local ForIterationValues = {
	StartValue = 0,
	EndValue = 1,
	Increasement = 0.1
}

for i = ForIterationValues.StartValue, ForIterationValues.EndValue, ForIterationValues.Increasement do
	-- This kind of generic for isn't one you really see often; the common ones you'll see a lot is a "i, v" set up; although, just to explain, 'part' is defined as the current iterated part.
	for part in pairs({game.Workspace.BasePlate}) do
		-- Yes, I put the BasePlate in the table like that, and gave to the generic for like that. Got a prob? Fite me. >>:C
		if part:IsA('BasePart') then
			-- The 'IsA' function, as explained before, will check if a object's class name, or type, is a certain type; for the above, 'BasePart' refers to all part types, being WedgePart, CornerWedgePart, Part, etc.
			part.Transparency = i
			-- Will set the object's transparency to 'i'
		end
	end
	coroutine.yield() -- You can use a regular "wait," but I'm doing it mah style, at least atm, rite now. >>;)
end

And that's pretty much it, believe it or not. e-e

However, to note, I didn't rewrite your script for you; that's your job. The above code(s) are only to be used, and are intended to be used, as references.

Stuff touched on, but got into detail about To go to the WIKI/ Documents on the below, click the highlighted words. :)

  1. The For loop.

  2. The Generic For

  • Well, I gotta touch on this: what the generic for does is iterate through a table, and it defines (if you have two variables, like "i, v", then you use "v", but if it's stand alone, you use that variable ONLY) the current iterated object w/ the/ a variable.
  1. The IsA Function.

  2. ClassName

  • Gotta touch on this too: this is a property that ALL objects have, and it tells/ shows the type the object is; say you want to know the WorkSpace's type, you just do print(Workspace.ClassName), and it should print something like Workspace. That's essential what does. :P However, you can NOT change the property value at all, otherwise your code will break.

Hope this helped you in any way! ;D

1 comment

0

2lazy2putin: Another noticeable problem is that you didn't set up the iterated for correctly; you set it up as "for i = start value, end value - 0.05," and not "for i = start value, end value, -0.05."

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