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

Problem when changing a part's position in a script. Any way to fix it?

Asked by
AZDev 590 Moderation Voter
9 years ago

The code itself works. When the code is executed, and a player or part is in the way it will move the part up.

--[[ position and rotation when door is open
R = -180, -0, -90
P = -6.505, 8, 19.495
]]--

--[[position and rotation when door is closed
R = 90, 90, 0
P = -4.6, 8, 17.5
--]]

function openDoor()
    if workspace.Door.DoorOpenBool.Value == false then
        --open the door
        script.Parent.DoorOpen.Rotation = Vector3.new(-180, -0, -90)
        script.Parent.DoorOpen.Position = Vector3.new(-6.505, 8, 19.495)
        workspace.Door.DoorOpenBool.Value = true
        else if workspace.Door.DoorOpenBool.Value == true then
            --close the door
            script.Parent.DoorOpen.Rotation = Vector3.new(90, 90, 0)
            script.Parent.DoorOpen.Position = Vector3.new(-4.6, 8, 17.5)
            workspace.Door.DoorOpenBool.Value = false
        end
    end
end

script.Parent.ClickDetector.MouseClick:connect(openDoor)

When you open the door and a part is where the door's position should change to, it will place it in a location where no parts block it. Is there any way to make it so that the position change is correct regardless of a part or player in that location? If not is there a way to make it ignore the code if there is an object in that location? If not that is there any workaround for this?

Thanks, and I hope this made since.

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

Changing the .Position and .Rotation property is almost never what you want -- it behaves how you described, moving up until there's room.


If you want a part to go where you ask it, you have to set it's CFrame -- short for CoordinateFrame.

CoordinateFrames are a position and an orientation (rotation).

    part.CFrame = CFrame.new(10, 20, 30)
        * CFrame.Angles( math.rad(40), math.rad(50), math.rad(60))

would be equivalent to

    part.Rotation = Vecto3.new(40, 50, 60)
    part.Position = Vector3.new(10, 20, 30)

Note that if the rotation isn't actually changing, e.g., how it start's is what you want, you can just change it relatively

    part.CFrame = part.CFrame - part.Position + Vector3.new(10, 20, 30)
Ad

Answer this question