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

How to use a for in pairs loop then a for loop to change properties of two parts at the same time?

I want this for loop to change the properties of multiple parts at the same time. In this case I'm changing the parts' transparencies and with this for loop it changes one part's transparency and then it changes the other after. I want to make it change both at the same time. Here is my script, please see if you can help me.

Local Script

local button = workspace.BUTTON.Activator
local seconds = 30

local bar = script.Parent.Frame.Bar
local text = bar.Parent.TextBox

local db = false

button.Touched:Connect(function(hit)
	if db == false then
		db = true
		print("Button was hit")
		if hit.Parent:FindFirstChild("Humanoid") then
			for i,v in pairs(workspace:GetChildren())do
				if v.Name == "button activated brick" then
					for i = 0, 1, 0.01 do
						v.Transparency = i
						if i == 1 then 
							db = false
							v.CanCollide = false
						end
						wait(seconds/100)
					end
				end
			end
		end
	elseif db == true then
		return
	end
end)

local timedb = false

button.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if timedb == false then 
			timedb = true
			for i = 30,0,-1 do
				Text.Text = i.." seconds"
				local percentage = i/30
				bar:TweenSize(UDim2.new(percentage,0,1,0),Enum.EasingDirection.Out, Enum.EasingStyle.Sine, 0.5)
				if i == 0 then
					timedb = false
				end
				wait(1)
			end
		end
	end
end)

Thanks for your time in reading this. I appreciate all effort going into answering this question.

6 comments

0

You could run the same code in another thread? though I don't think it'd be efficient performance wise

0

I tried that but it broke. I'm not quite sure why though.

0

So are you basically saying that there was a time delay between making the first part transparent and the second part?

0

Yes

0

Well, not really actually

0

What it did was first make one part transparent then after 30 seconds, once that was fully transparent it started making the other part transparent. I want them both to happen at the same time.

2 answers

local button = workspace.BUTTON.Activator
local seconds = 30

local db = false

button.Touched:Connect(function(hit)
    if db == false then
        db = true
        print("Button was hit")
        if hit.Parent:FindFirstChild("Humanoid") then
            for i,v in pairs(workspace:GetChildren())do
		spawn(function()
	                if v.Name == "button activated brick" then
	                    for i = 0, 1, 0.01 do
	                        v.Transparency = i
	                        if i == 1 then 
	                            db = false
	                            v.CanCollide = false
	                        end
	                        wait(seconds/100)
	                    end
	                end
		end)
            end
        end
    elseif db == true then
        return
    end
end)

1 comment

0

Thanks for the help. This works but it only works once. After it has been done once it never works again

When it comes to changing properties smoothly, I would recommend you use TweenService instead of using an for loop to make parts fade.

local TweenService = game:GetService('TweenService')
local button = workspace.BUTTON.Activator
local seconds = 30

local db = false

local tInfo= TweenInfo.new(seconds)
local properties = {Transparency = 1}

button.Touched:Connect(function(hit)
	if (db == true) then
		return
	end
	db = true
	print("Button was hit")
	if hit.Parent:FindFirstChild("Humanoid") then
		for i,part in pairs(workspace:GetChildren())do
			if part.Name == "button activated brick" then
				local fade = TweenService:Create(part,tInfo,properties)
				fade.Completed:Connect(function()
					db = false
					part.CanCollide = false
				end)
				fade:Play()
			end
		end
	end
end)

2 comments

0

This works well but now the next part of my script doesn't work. It's meant to make a bar which gets smaller and there is textbox that changes it text on how many seconds left. Any help?

0

I edited my answer

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