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

What's wrong with this CFrame script?

Asked by 8 years ago
local door = workspace.CFrameDoor
local moved = false

--63, 3.9, -100.1
script.Parent.ClickDetector.MouseClick:connect(function()
    if moved == false then
        moved = true
        for x = 63, 59, -1 do
            door.CFrame = door.CFrame * (CFrame.new(x, 0, 0))
            wait(.1)
        end
    end
end)

I want it to move -4 studs but it moves out of sight.

1 answer

Log in to vote
0
Answered by 8 years ago

Relative CFraming

When you use the multiplication operator * with two CFrames, you are basically taking the first CFrame and moving that by the second CFrame.

For example:

print(CFrame.new(0,1,0) * CFrame.new(0,5,0))
--0, 6, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1

From the following, you'd notice that the first three numbers (represents position), are based on how we multiplied the CFrames. We took the lookVector of the first CFrame by just stating that is the CFrame we want to base our new CFrame off of, and we multiplied by a certain number to atain a new CFrame. As you can see, the new Y axis number for the position is 6. Everything that was done with multiplication is basically adding the positions inside the CFrame.

In this same manner, we can see what is wrong with your logic. You aren't multiplying your CFrame by '1' 4 times, but rather you are multiplying it by numbers from 59..63

To fix this, we just make the loop only run four times.

local door = workspace.CFrameDoor
local moved = false

--63, 3.9, -100.1
script.Parent.ClickDetector.MouseClick:connect(function()
    if moved == false then
        moved = true
        for x = 1,4 do
            door.CFrame = door.CFrame * (CFrame.new(1, 0, 0))
            wait(.1)
        end
    end
end)

A basic Lua-To-English translation of the new edit is that 4 times, we move the CFrame 1 stud on the X Axis.


Useful Links:

CFrame : http://wiki.roblox.com/index.php?title=CFrame

Lookvector: http://wiki.roblox.com/index.php?title=CFraming#lookVector

Ad

Answer this question