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

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

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

✓ Accepted answer

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)

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