I, v in pairs() only loops once

Basically, I’m trying to make a door that you can unlock by triggering a proximityprompt.
However, out of all the parts, only 1 becomes transparency 0.6 and after that, it selects another random part?

Script:

local house = script.Parent
local prox = Instance.new("ProximityPrompt")
for i, v in pairs(house:GetDescendants()) do
	if v:IsA("BasePart") and v.BrickColor == BrickColor.new("Really black") then
		prox.Parent = v
		prox.ActionText = "Open"
		prox.ObjectText = "Door"
		prox.RequiresLineOfSight = false
		prox.HoldDuration = 5
		prox.MaxActivationDistance = 12
	end
end

prox.Triggered:Connect(function()
	local door = prox.Parent.Parent
	
	for i, v in pairs(door:GetChildren()) do
		if v:IsA("BasePart") and v.Color ~= Color3.new(160, 95, 53) then
			v.Transparency = .6
			v.CanCollide = false
			wait(6)
			v.Transparency = 0
			v.CanCollide = true
		end
	end
end)

you need to spawn it in a new thread so that it doesn’t get delayed by the 6 second wait you have.

for i, v in pairs(door:GetChildren()) do
	task.spawn(function()
		if v:IsA("BasePart") and v.Color ~= Color3.new(160, 95, 53) then
			v.Transparency = .6
			v.CanCollide = false
			wait(6)
			v.Transparency = 0
			v.CanCollide = true
		end
	end)
end
2 Likes

Hello! The wait(6) in your loop is the one that’s making it work on one part to another in a certain amount of time, I recommend using a coroutine or spawn() function.

1 Like

Exactly what I needed, thanks for your help

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.