For i, loop only working for one part

I have this script that should activate for all parts. But it will only activate for one.

local UIS = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local humRoot = char:WaitForChild("HumanoidRootPart")
local camera = game.Workspace.CurrentCamera

local proxPromt = script.Parent

for _, prompt in pairs(game.Workspace:GetDescendants()) do
	if prompt:IsA("ProximityPrompt") then
		print("TRUE")
		local BillboardGui = prompt.Parent:WaitForChild("BillboardGui")
		local BillboardImage = BillboardGui:WaitForChild("ImageLabel")

		local Tween  = TweenService:Create(BillboardImage, TweenInfo.new(0.2, Enum.EasingStyle.Exponential), {
			ImageColor3 = Color3.fromRGB(166, 166, 166)
		})
		local Tween2  = TweenService:Create(BillboardImage, TweenInfo.new(0.2, Enum.EasingStyle.Exponential), {
			ImageColor3 = Color3.fromRGB(255, 255, 255)
		})

		BillboardGui.Enabled = false

		prompt.PromptShown:Connect(function ()
			BillboardGui.Enabled = true
		end)

		prompt.Triggered:Connect(function ()
			Tween:Play()
			Tween.Completed:Wait()
			task.wait(0.2)
			Tween2:Play()
			Tween2.Completed:Wait()
		end)

		prompt.PromptHidden:Connect(function ()
			BillboardGui.Enabled = false
		end)
	end
end

How could i fix this?

The code looks fine, perhaps it’s getting stuck waiting forever on a :WaitForChild() somewhere? Do you have any ProximityPrompts in workspace that perhaps don’t have a sibling BillboardGui with an ImageLabel?

1 Like

hmm thats weird, i switched it to FindFirstChild and now it works perfectly🤨

nvm, it worked perfectly only the first time

How are you judging if it “works”? Only one TRUE is printing?

1 Like

no it printed true 4 times, but then it just decided to quit on me

You can test to see if it’s a yielding problem by adding a task.spawn() like so

local TweenService = game:GetService("TweenService")
local Workspace = game:GetService("Workspace")

local ACTIVATED_COLOR = Color3.fromRGB(166, 166, 166)
local DEFAULT_COLOR = Color3.fromRGB(255, 255, 255)
local ACTIVATION_TIME = 0.2

local tweenInfo = TweenInfo.new(ACTIVATION_TIME, Enum.EasingStyle.Exponential)

local function setupPrompt(prompt: ProximityPrompt)
	local billboardGui: BillboardGui = prompt.Parent:FindFirstChild("BillboardGui")
	assert(billboardGui, `Missing BillboardGui next to {prompt:GetFullName()}`)

	local imageLabel: ImageLabel = billboardGui:FindFirstChild("ImageLabel")
	assert(imageLabel, `Missing ImageLabel in {billboardGui:GetFullName()}`)

	local activatedTween = TweenService:Create(imageLabel, tweenInfo, {
		ImageColor3 = ACTIVATED_COLOR
	})
	local defaultTween = TweenService:Create(imageLabel, tweenInfo, {
		ImageColor3 = DEFAULT_COLOR
	})

	prompt.PromptShown:Connect(function()
		billboardGui.Enabled = true
	end)

	prompt.PromptHidden:Connect(function()
		billboardGui.Enabled = false
	end)

	prompt.Triggered:Connect(function()
		activatedTween:Play()
		activatedTween.Completed:Wait()
		task.wait(ACTIVATION_TIME) -- Are you intentionally waiting additional time after the tween completes?
		defaultTween:Play()
		defaultTween.Completed:Wait()
		-- Assuming you have more code here, otherwise you don't need the above wait
	end)
end

local function setupAllPrompts(parent: Instance)
	for _, prompt in parent:GetDescendants() do
		if not prompt:IsA("ProximityPrompt") then
			continue
		end

		task.spawn(setupPrompt, prompt)
	end
end

setupAllPrompts(Workspace)

This should show errors in the output if there are missing expected instances

ohh i think thats what it was, i got it figured out