For Loop not running?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I want run the “for” loop

  1. What is the issue? Include screenshots / videos if possible!

Its just not running

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local obby = workspace:WaitForChild("Obby")
local Cave = obby:WaitForChild("Cave")
local Gameplayer = Cave:WaitForChild("Gameplay")
local Gate = Gameplayer:WaitForChild("Gate")
local RunesTable = {}
local CorrectOrder = {"Rune3", "Rune1", "Rune4", "Rune2"} 

local Runes = workspace:WaitForChild("RuneStand"):WaitForChild("ClickRunes")

local TweenService = game:GetService("TweenService")

local issolved = false

local function isOrderCorrect()
	for i, rune in ipairs(RunesTable) do
		if rune ~= CorrectOrder[i] then
			return false
		end
	end
	return true
end

for i, Rune in ipairs(Runes:GetChildren()) do
	for i, Detector in ipairs(Rune:GetChildren()) do
		Detector.MouseClick:Connect(function()
			print("puzzle")
			if not table.find(RunesTable, Rune.Name) then
				if #RunesTable >= 4 then
					print("Table is full")
				else
					table.insert(RunesTable, Rune.Name)
					if Rune.Name == "Rune1" then
						Rune.Color = Color3.new(0.341176, 1, 0.223529)
					elseif Rune.Name == "Rune2" then
						Rune.Color = Color3.new(1, 0.0823529, 0.0823529)
					elseif Rune.Name == "Rune3" then
						Rune.Color = Color3.new(0.14902, 0.333333, 1)
					elseif Rune.Name == "Rune4" then
						Rune.Color = Color3.new(1, 0.894118, 0.298039)
					end


					Rune.Material = Enum.Material.Neon
				end

				if #RunesTable == #CorrectOrder then
					if isOrderCorrect() then
						issolved = true
						print("Correct order!")

						local start_cf = Gate:GetPivot()
						local end_position = Vector3.new(1157.384, 859.865, 2762.123)

						local obj = Instance.new("NumberValue")
						obj.Value = 0 

						local openDown = TweenService:Create(obj, TweenInfo.new(4, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {Value = 1})

						obj:GetPropertyChangedSignal("Value"):Connect(function()

							local tween_position = start_cf.Position:Lerp(end_position, obj.Value)
							local tween_cf = CFrame.new(tween_position) * start_cf.Rotation


							Gate:PivotTo(tween_cf)
						end)

						openDown:Play()
					else
						print("Incorrect order!")
						task.wait(0.5)
						for i , rune in ipairs(Runes:GetChildren()) do
							rune.Color = Color3.new(0.278431, 0.278431, 0.278431)
						end
						table.clear(RunesTable)
						print(RunesTable)
					end
				end
			else
				print("Rune already added")
			end
			print(RunesTable)
		end)
	end
end

image

There are multiple for loops in the script you provided, which one of them are you having trouble with?

all of them , so i tried adding a task.wait before loop , and loop is running , but sometimes not. How can I handle it properly?

Hello your script seems fine to me Maybe its an Infinite Yield problem did you try to wait after running the game for about 10 seconds ?? and see what the output + please ss output

Already checked for infinite yiels , eveything is fine

No Errors in output ? nothing at all ?

Where do you have the Script Located + And is this a local script or server script

script is local , located in starterplayerscripts. And no error in output. As I said above , i tried to add a task wait before loop for 5 seconds , and everything worked fine , sometimes not though.

That simply means that the Runes are not loading in time they are being loaded after the code has been run

You can fix this by Using Tags and tag Service because you can detect if an object with a specific tag has been added

1 Like

Can you print if game:IsLoaded()

Printing is also not working , whats wrong!!!

I Was aware of this can you print anything on the top of the script?

Ye , only when getting variables , then not

Let me revamp the code real quick

image
Also your code works fine for me i tried it

local TweenService = game:GetService("TweenService")

local RunesTable = {}
local CorrectOrder = {"Rune3", "Rune1", "Rune4", "Rune2"} 

local Runes = workspace:WaitForChild("RuneStand"):WaitForChild("ClickRunes")

local issolved = false

warn("HERE 1")

local function isOrderCorrect()
	for i, rune in ipairs(RunesTable) do
		if rune ~= CorrectOrder[i] then
			return false
		end
	end
	return true
end

warn("HERE 2")

for i, Rune in ipairs(Runes:GetChildren()) do
	for i, Detector in ipairs(Rune:GetChildren()) do
		Detector.MouseClick:Connect(function()
			print("puzzle")
			if not table.find(RunesTable, Rune.Name) then
				if #RunesTable >= 4 then
					print("Table is full")
				else
					table.insert(RunesTable, Rune.Name)
					if Rune.Name == "Rune1" then
						Rune.Color = Color3.new(0.341176, 1, 0.223529)
					elseif Rune.Name == "Rune2" then
						Rune.Color = Color3.new(1, 0.0823529, 0.0823529)
					elseif Rune.Name == "Rune3" then
						Rune.Color = Color3.new(0.14902, 0.333333, 1)
					elseif Rune.Name == "Rune4" then
						Rune.Color = Color3.new(1, 0.894118, 0.298039)
					end


					Rune.Material = Enum.Material.Neon
				end

				if #RunesTable == #CorrectOrder then
					if isOrderCorrect() then
						issolved = true
						print("Correct order!")

						local end_position = Vector3.new(1157.384, 859.865, 2762.123)

						local obj = Instance.new("NumberValue")
						obj.Value = 0 

						local openDown = TweenService:Create(obj, TweenInfo.new(4, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {Value = 1})

						obj:GetPropertyChangedSignal("Value"):Connect(function()
						end)

						openDown:Play()
					else
						print("Incorrect order!")
						task.wait(0.5)
						for i , rune in ipairs(Runes:GetChildren()) do
							rune.Color = Color3.new(0.278431, 0.278431, 0.278431)
						end
						table.clear(RunesTable)
						print(RunesTable)
					end
				end
			end
		end)
	end
end

warn("HERE 3")

ye sure , but Is it ok to share my discord here?Doesnt it break some forum rules?

just say the name don’t say it’s something associated

Runes:GetChildren() is an empty table {} the descendants were not replicated at the time of inquiry and thus there is nothing to loop through

also why are you using a second for loop to access a single click detector? Is there going to be more than one?



^^ I meant collectionservice lol