This makes a brick that randomly goes up and down like the bars in this video. It has been working but if I stand on top of them they will teleport on top of me. To try to fix that, I've limited how far it goes up if someone's touching it, but the script is breaking. There are no errors, but whenever the brick is touched the brick stops. Any ideas why?
touched = false
prevnum = 0
script.Parent.Touched:connect(function()
touched = true
end)
script.Parent.TouchEnded:connect(function()
touched = false
end)
while touched == false do
wait(.05)
script.Parent.CanCollide = false
a = math.random(3, 3)
script.Parent.Size = Vector3.new(script.Parent.Size.X, a, script.Parent.Size.Z)
script.Parent.Position = Vector3.new(script.Parent.Position.X, (a / 2) , script.Parent.Position.Z)
prevnum = a
script.Parent.CanCollide = true
end
while touched == true do
wait(.05)
if prevnum == 3 then
script.Parent.CanCollide = false
b = math.random(3, 4)
script.Parent.Size = Vector3.new(script.Parent.Size.X, b, script.Parent.Size.Z)
script.Parent.Position = Vector3.new(script.Parent.Position.X, (b / 2) , script.Parent.Position.Z)
print(prevnum)
prevnum = b
print(prevnum)
script.Parent.CanCollide = true
elseif prevnum == 4 then
script.Parent.CanCollide = false
b = math.random(3, 5)
script.Parent.Size = Vector3.new(script.Parent.Size.X, b, script.Parent.Size.Z)
script.Parent.Position = Vector3.new(script.Parent.Position.X, (b / 2) , script.Parent.Position.Z)
prevnum = b
script.Parent.CanCollide = true
elseif prevnum == 5 then
script.Parent.CanCollide = false
b = math.random(3, 6)
script.Parent.Size = Vector3.new(script.Parent.Size.X, b, script.Parent.Size.Z)
script.Parent.Position = Vector3.new(script.Parent.Position.X, (b / 2) , script.Parent.Position.Z)
prevnum = b
script.Parent.CanCollide = true
elseif prevnum == 6 then
script.Parent.CanCollide = false
b = math.random(3, 7)
script.Parent.Size = Vector3.new(script.Parent.Size.X, b, script.Parent.Size.Z)
script.Parent.Position = Vector3.new(script.Parent.Position.X, (b / 2) , script.Parent.Position.Z)
prevnum = b
script.Parent.CanCollide = true
elseif prevnum == 7 then
script.Parent.CanCollide = false
b = math.random(3, 7)
script.Parent.Size = Vector3.new(script.Parent.Size.X, b, script.Parent.Size.Z)
script.Parent.Position = Vector3.new(script.Parent.Position.X, (b / 2) , script.Parent.Position.Z)
prevnum = b
script.Parent.CanCollide = true
end
end

2 comments
Instead of script.Parent.Position, use script.Parent.CFrame to avoid that. Position checks for collisions when being set, and will move the part to the nearest point above the defined point where it will fit. CFrame doesn't bother with that.
Vector3 clips, so when you change the Position of a part (Which requires a Vector3 value) and there is a part in the way, the part will get moved on top of the other one. CFrame doesn't clip.