Code After For Loop Doesn't Run

The code detects that if you’re at reach of a wall, you’ll be able to hide in a grass wall, so it goes through a process, hides in the wall, and then does a loop that makes the character invisible and non-Collidable, but then after a cooldown I want it to bring them back to their past original state, but the first loop doesn’t allow the next lines to run.

9690749e614537ff1363a4816948c8ad

As you can see it does enter the wall, makes them invisible, but then it doesn’t bring them back to the original position and turns the parts visible. (Cooldown is set to 2)

If you need more information please ask right away, thank you so much!

if onReach then
						-- Hide in Wall --
						print("passing")
						
						local oldPos = rootPart.CFrame

						rootPart.Anchored = true 
						rootPart.CFrame = CFrame.new(action,rootPart.Position) + Vector3.new(1.5 * rootPart.CFrame.LookVector.X,0,1.5 * char.HumanoidRootPart.CFrame.LookVector.Z)
						char.Humanoid.AutoRotate = false
						
						
						for i, parts in pairs(char:GetChildren()) do
							if parts.ClassName ~= "Part" then return end
							
							PhysicsService:SetPartCollisionGroup(parts, "Hidden")
							parts.Transparency = 1
						end
						
						print("running")

						wait(hiddenDuration)
						

						rootPart.Anchored = false 
						rootPart.CFrame = oldPos
						char.Humanoid.AutoRotate = true

						for i, items in pairs(char:GetChildren()) do
							if items.ClassName ~= "Part" then return end
							
							PhysicsService:SetPartCollisionGroup(items, "Default")
							items.Transparency = 0
						end

					end

I still haven’t been able to find the solution and do not understand why it’s doing this

You are returning instead of breaking. Returning stops the entire function / script

image

use continue instead.

if onReach then
	-- Hide in Wall --
	print("passing")

	local oldPos = rootPart.CFrame

	rootPart.Anchored = true 
	rootPart.CFrame = CFrame.new(action,rootPart.Position) + Vector3.new(1.5 * rootPart.CFrame.LookVector.X,0,1.5 * char.HumanoidRootPart.CFrame.LookVector.Z)
	char.Humanoid.AutoRotate = false


	for i, parts in pairs(char:GetChildren()) do
		if parts.ClassName ~= "Part" then 
			continue -- doesnt break the loop and also does not stop execution
		end

		PhysicsService:SetPartCollisionGroup(parts, "Hidden")
		parts.Transparency = 1
	end

	print("running")

	wait(hiddenDuration)


	rootPart.Anchored = false 
	rootPart.CFrame = oldPos
	char.Humanoid.AutoRotate = true

	for i, items in pairs(char:GetChildren()) do
		if items.ClassName ~= "Part" then 
			continue -- doesnt break the loop and also does not stop execution
		end

		PhysicsService:SetPartCollisionGroup(items, "Default")
		items.Transparency = 0
	end

end

This is how continue works:

for i = 1, 10 do
	if i == 5 then
		continue
	end
	print(i)
end

-- Prints: 1, 2, 3, 4, 6, 7, 8, 9, 10

If you want to stop the loop completely, use break. If you want to skip that certain iteration of the loop, use continue

2 Likes

Thank you so much- my logic was flawed :joy::joy: my brain overheats after I code so much, and then i stop going for the simple. Thank you so much I appreciate it, I will fix it tommorow(later today) since it’s 1:30 am right now

1 Like