Can someone explain this complicated line of code?

This is a door script I found on Youtube that allows the door the move upwards when a button is pushed.

local TweenService = game:GetService("TweenService")
local door = script.Parent.Parent.Door
local doorButton = script.Parent
local clickDetector = script.Parent.ClickDetector
local doorClosed = true

local doorClosedPosition = door.Position
local doorClosedSize = door.Size

clickDetector.MouseClick:Connect(function()
	local doorTweenInfo = TweenInfo.new(2)
	local endPosition = {}
	if doorClosed == true then
		endPosition.Position = Vector3.new(door.Position.X, door.Position.Y + door.Size.Y/2, door.Position.Z)
		endPosition.Size = Vector3.new(door.Size.X, 0, door.Size.Z)
		doorButton.BrickColor = BrickColor.new("Really red")
		doorClosed = false
	else
		endPosition.Position = doorClosedPosition
		endPosition.Size = doorClosedSize
		doorButton.BrickColor = BrickColor.new("Lime green")
		doorClosed = true
	end
	local tween = TweenService:Create(door, doorTweenInfo, endPosition)
	tween:Play()
end)

What does “Door.Position.x/y/z” mean and do? Why do I have to do “door.Position.Y + door.Size.Y/2”? Well I get it’s so that door goes uptop instead of the middle but how does adding the size divided by 2 cause it to do that?

1 Like

Vector3 is its own data type along with CFrame and Ray. Contains information about position in 3D space. More information here: Vector3 | Documentation - Roblox Creator Hub

Because Door is apparently a BasePart, its Position and Size property uses Vector3.

1 Like

The position of the door is always in the middle of it. So in order to position it on top of the ground, I have to make the door be half of it’s own size higher. I’ll draw a picture in case my wording is kind of hard to understand

2 Likes

Normally the door’s position will be going through the floor?

Here is the line by line explantion of the code!

1 Like

It depends on how you set the door’s properties to be closed; also applies to the opened properties.

Yes (sorry if my paint skills are a little wonky lol)

What does “door.Position.x” do? Does it change the value or have it remain the same?

It gets the position on the x axis of the Door