Help on repeat until

I’m trying to write a script that makes a part go down until it touches another brick. It still seems to go down even after it touches another brick. Here’s my script.

local test = false

script.Parent.Touched:connect(function(hit)
	test = true
end)

repeat
	script.Parent.CFrame = script.Parent.CFrame + Vector3.new(0, -0.1, 0) --You can change the value here to change how the block expands.
	wait(0.001)
until test == true

I’m not sure where in the script I messed up, or if I forgot to add something

1 Like

your script looks right.The wait() delay is too fast so it could be the problem

2 Likes

It’d be interesting to know also the size of your parts. If they are nanometric no wonder why nothing happens: collisions aren’t properly calculated due to your parts skipping one another.
Also, I personally hate repeat until. I suggest doing something like:

local test = false;

script.Parent.Touched.Connect(function(hit) -- it's "Connect", not "connect"
   test
end)

while not test do -- first checks the condition then runs the code, unlike repeat until loops
   script.Parent.CFrame =  script.Parent.CFrame - Vector3.new(0,.1,0); -- looks better with positive arguments
   task.wait(); -- task.wait() is more precise than wait()
end

Also please notice that it’s “Connect”, not “connect”,

1 Like

Ok I changed the script to:

local test = false;

script.Parent.Touched:Connect(function(hit)
	test = true;
end)

while not test do
	script.Parent.CFrame =  script.Parent.CFrame - Vector3.new(0,.1,0);
	task.wait();
end

Here’s how it turned out (The script is on the blue brick)


I’m not sure why its not working

1 Like

Please can you show us the properties of your blue brick? I suspect you turned off the .CanTouch property.

1 Like

image
Same on both bricks

1 Like

Please can you turn them off and re-activate? Make sure it’s on for both. I don’t exactly remember what’s the right symbol, but I remember that when at least one of multiple selections, but not all, have that property on it’d show some white stuff.

1 Like

Your beginning script was almost perfect. I would change wait(0.001) to use task.wait(), and instead of using a Touched event you can just check for :GetPartsInPart().

repeat
	script.Parent.CFrame = script.Parent.CFrame - Vector3.new(0, 0.1, 0)
	task.wait()
until #game.Workspace:GetPartsInPart(script.Parent) > 0

The Touched event fires upon a physics interaction; since you’re moving the part programmatically into another anchored part, Touched will not fire.

Oh I was not aware about the .Touched event not firing when you moved parts this way. Thanks for teaching me smth new. <3

Touched does fire when you move an anchored object programmatically into an unanchored part, too. It only behaves this way when both parts are not affected by physics.

1 Like

Touched is a physics based connection (Has to be moving). Since artificially changing the position isn’t considered moving, use GetPartsInPart if you are trying to tell if they are touching.

Thanks, this worked very well! I’m tryna make an artificial physics engine with a script btw

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