Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does my script teleport the part up?

Asked by
Jexpler 63
5 years ago

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
try using CFrame instead of vector3 positions as CFrames aren't affected by collisions and also try using tween service or CFrame lerp theking48989987 2147 — 5y

2 answers

Log in to vote
1
Answered by
xPolarium 1388 Moderation Voter
5 years ago
Edited 5 years ago

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.

0
It kept going on forever Jexpler 63 — 5y
0
It also teleported above my structure still. Jexpler 63 — 5y
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. xPolarium 1388 — 5y
0
Try it now. Changed it so it changes the CFrame of the part. xPolarium 1388 — 5y
View all comments (4 more)
0
It says it expected a Vector3, got CFrame Jexpler 63 — 5y
0
oops, overlooked that xPolarium 1388 — 5y
0
It works. I'm just having trouble getting it to go to exactly where I want it to. Jexpler 63 — 5y
0
I was able to get it by messing with the starting position. Jexpler 63 — 5y
Ad
Log in to vote
1
Answered by 5 years ago

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

Answer this question