Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does my script teleport the part up?

I have a script that's supposed to move a part. For some reason, it teleports it up. Why?

script.Parent.ClickDetector.MouseClick:Connect(function(plr)
	for i = 1, 52 do
		local door1 = script.Parent.Parent.Door1
		local x1 = door1.Position.X
		local y1 = door1.Position.Y
		local z1 = door1.Position.Z
		local newx1 = x1 + .1
		door1.Position = Vector3.new(newx1,y1,z1)
	end
end)

1 comment

1

try using CFrame instead of vector3 positions as CFrames aren't affected by collisions and also try using tween service or CFrame lerp

2 answers

✓ Accepted answer

I believe you're looking to tween the part to move upwards. What's happening is that the loop finishes before you can see it. Add a wait() inside the loop if you'd like to see it.

Also your for-loop has an increment parameter than you can use instead of adding .1 to the position X value every loop.

--This doesn't need to be in the loop.
local door1 = script.Parent.Parent.Door1

script.Parent.ClickDetector.MouseClick:Connect(function(plr)
	--This for loop is saying: From 1 count to 52 by .1
	for i = 1, 52, .1 do
		--Since we're only adding .1 to get to 52 we can just use a new
		--Vector3 and add it to our door's current position
		door1.CFrame = door1.CFrame + Vector3.new(i, 0, 0)
	
		wait() --so we can see it move.
	end
end)

Alternatively and even more effectively, you should be using TweenService to tween parts from one position to the next.

RobloxDev example on tweening a part.

8 comments

0

It kept going on forever

0

It also teleported above my structure still.

0

You need to play with the values in the for loop. This is adding .1 until the part's x position reaches 52 studs. Which, if you think about it, is a long distance for a door.

0

Try it now. Changed it so it changes the CFrame of the part.

0

It says it expected a Vector3, got CFrame

0

oops, overlooked that

0

It works. I'm just having trouble getting it to go to exactly where I want it to.

0

I was able to get it by messing with the starting position.

Use this instead

script.Parent.ClickDetector.MouseClick:Connect(function(plr)
    for i = 1, 52 do
        local door1 = script.Parent.Parent.Door1
        local x1 = door1.Position.X
        local y1 = door1.Position.Y
        local z1 = door1.Position.Z
        local newx1 = x1 + .1
        door1.CFrame = CFrame.new(newx1,y1,z1)
    end
end)

You can't teleport move things using the Position property, use CFrame and it's recommended to use tweening instead

This discussion is preserved as it was. New answers, comments, and votes are closed.