Can someone explain this line of code to me?

I found this door opening script on youtube that causes the door to move upwards when a button is clicked, but I don’t understand a line. Here’s the full script:

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)

Here’s the part I don’t get:

endPosition.Position = Vector3.new(door.Position.X, door.Position.Y + door.Size.Y/2, door.Position.Z)

Why is the Y position added onto Y Size divided by 2? The vid I found this from didn’t explain it very clearly.

Vid link for reference: https://www.youtube.com/watch?v=olYzR9KdJ-w&t=270s

2 Likes


As you want to position the closed part at the top of the door, and not in the middle you take 1/2 of the doors Y size.

5 Likes

What do you mean by closed part of the door?

Oops, I meant that when the door is closed, you want to position it at the top instead of the middle.

I see, thanks for the explanation. However to make the door go from bottom to top, do I do - the Y size/2 like in your short clip?

Lucke is correct but I will add an illustration.

It makes it so that the door is in position 2 instead of position 1 when opened.

image

I removed the “/2” to see what it looks like but it’s still the same as it is without?

Hmmm, that should affect the door. I wonder why it didn’t. Do you have another instance of the script somewhere? It’s a sliding door correct?

I realised the door just slides upwards and through the frame as opposed to shrinking to oblivion. To be honest I still don’t 100% understand the code of adding the size to position then dividing by 2, then have 0 in the next line when defining the size for y. I get it is so that the door goes back uptop instead of the middle but how does it do that, as in why adding the size divided by 2 will cause it to do that? Also by writing “door.Position.x”, does it mean the X coordinate remains unchanged?