If statement not being ignored in 1st loop, but 2nd loop

I am trying to make a moving platform in my game.
It looks like this:


But I have a problem I cant figure out
So for some reason for the 1st loop, the detection works. But then in the second loop, the if statement is completely ignored.
Here is my code:

script.Parent.Touched:connect(function()
	local y = 4.5
	script.Parent.Color = Color3.new(0, 0, 0)
	script.Parent.CanTouch = false
	script.Parent.Transparency = 0.2
	script.Parent.CanCollide = false
	while true do
		wait(0.1)
		script.Parent.Parent.Platform_1.Position = Vector3.new(-21, y, -223)
		y += 1
		--check if the platform is there
		if script.Parent.Parent.Platform_1.Position == Vector3.new(-21, 37.5, -223) then
			wait(5)
			y= 37.5
			while true do
                -- second loop
				wait(0.1)
				if script.Parent.Parent.Platform_1.Position == Vector3.new(-24, 4.5, -223) then
                   --Does not work (ignored if statement
					print('stop')
					break
				else
                    --Only this runs
					print('going down')
					y -= 1
					script.Parent.Parent.Platform_1.Position = Vector3.new(-21, y, -223)
				end
			end
		end
	end
end)

I have tried putting the movement code that makes it go down somewhere else and recently added the else statement, but it still does not work. I checked the position to make sure the position is exact also as the one the script is checking, but it still does not work.
Can anyone help?

You should not check the entire vector in your if statements. Only check the part that you know can vary between checks. In this case it seems you should be checking ‘y’ instead. Can you explain what the intended behavior is?

1 Like

have you tried using “tween service”?

TweenService | Roblox Creator Documentation

because in your case of what I’m gathering by watching your video there. using the tweening that part would be much cleaner for your code and most likely remove your frustration with what your current code says and what you’re showing it does.

Thank you, checking the y worked.

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