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

How do I combine a math.random with a for loop in the same line?

So, I have a model with plates in it. The script is supposed to make the plates transparent randomly, but I want the parts to go transparent slowly. Here's the script:

local model = game.Workspace.plates
local parts = model:GetChildren()
local db = false


while true do
	for i = 0,1,0.01 do
		parts.Transparency = math.random[i(1,#parts)]
		wait(0.01)
	end
	wait(1)
end

Where do I properly insert "i"? And is this script I made have anything else wrong? All help is appreciated!

1 comment

0

So are you asking for only one random part or a loop for all parts to indavidualy fade?

2 answers

✓ Accepted answer

I is the current number, so you probably want it to be the for loop like this:

local model = game.Workspace.plates
local parts = model:GetChildren()
local db = false

local curpart = parts[math.random(1, #parts)]
for i = 0,1,0.01 do
	curpart.Transparency = i
	wait(0.01)
end

2 comments

0

I want a random part to go transparent. Not ALL of them. There are 30 parts, and I want only one at a time to go transparent. That's why I'm using math.random

0

Edited answer, check if that's better.

You did it totally right, except for this:

parts.Transparency = math.random[i(1,#parts)]

Not sure what you were thinking there. It's wrong on so many levels xD.

There's no need to do it 'on the same line'. You can just use multiple lines:

local chosenPart = parts[math.random(#parts)]
chosenPart.Transprency = i

You should be able to figure out where these two lines belong. Just think logically.

Comment if you can't figure it out

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