Sliding Doors from GUI Button

  1. What do you want to achieve? I want to make sliding doors open and close with one gui button.

  2. What is the issue? I have no idea how to do it :stuck_out_tongue:

  3. What solutions have you tried so far? I have watched multiple videos.

1 Like

Make a click function for the button to tween the doors in the direction you want them to go.

In order to create a sliding door, that opens from a GUI button requires a few things

First lets create our door in Workspace
I have created a very basic door to suit our purpose


image

Now we need a UI button
image

This will require 2 scripts, a local script to control the GUI and a Server script (or just Script) to control the door
What we will do is put a local script under the OpenDoor Gui and a server script under the door, in addition we will add a remote event which we will use to connect the scripts later
image
image

Now lets get coding!

For our local script, we will want to fire the remote event. When we fire it, it will trigger the other script!

local remoteEvent = game.Workspace.Door:WaitForChild("RemoteEvent") -- We use wait for child to make sure that the client has the remote event replicated in workspace. 
local debounce = false --This is just to prevent spamming and lag on the server
-- Although it wont help against exploiters 
script.Parent.OpenButton.MouseButton1Click:Connect(function()
	if debounce == false then
		debounce = true
		remoteEvent:FireServer()
		wait(1)
        debounce = false
	end
end)

Now lets get to our server script

lremoteEvent.OnServerEvent:Connect(function()
	if doorOpen == false and movingDoors == false then -- We must open the door!
		doorOpen = true 
		movingDoors = true
		for i=1,5,StudsPer do
			script.Parent.Right.Position = script.Parent.Right.Position + Vector3.new(StudsPer,0,0)
			script.Parent.Left.Position = script.Parent.Left.Position - Vector3.new(StudsPer,0,0)
			wait(0.1)
		end
		movingDoors = false
	elseif doorOpen == true and movingDoors == false then
		doorOpen = false 
		movingDoors = true
		for i=1,5,StudsPer do
			script.Parent.Right.Position = script.Parent.Right.Position - Vector3.new(StudsPer,0,0)
			script.Parent.Left.Position = script.Parent.Left.Position + Vector3.new(StudsPer,0,0)
			wait(0.1)
		end
		movingDoors = false
	end
end)

Tada, we now have an open-able door.
Note that if you rotate the door, you will have to change these lines

script.Parent.Right.Position = script.Parent.Right.Position - Vector3.new(StudsPer,0,0)
script.Parent.Left.Position = script.Parent.Left.Position + Vector3.new(StudsPer,0,0)

You can also lerp the door for better smoother movement

Openable door.rbxl (21.8 KB)

Edit: Fixed the variable bug

4 Likes

Thank you for the script. The problem is, you can only open and close it once. How do you do it so you can do it a bunch of times?

Whoops, looks like I forgot to change the variable in the closing feature, silly mistake
Try this one, I have also edited the post’s server script to fix that
Openable door.rbxl (21.8 KB)

2 Likes

It works! The issue is that inside of my part(s) there is a union (which is part of the door). It isn’t moving with the part itself. How do I do that?

You either need to then tween the union again, as models cannot be tweened right now. And you just use tween!

local tweenservice = game:GetService("TweenService")
local doorRight = game.Workspace.Door.Right -- don't judge me for weird variable names lol
local doorLeft = game.Workspace.Door.Left
local guiButton = script.Parent -- make sure to put this script in localScript under the textbutton

local tweeninginfo = TweenInfo.new(5,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut,1,false,0.5)
local propRight = {Vector3.new(--[[Position of the right door]])}
local propLeft = {Vector3.new(--[[again do the pos of your choice]])}

local tween = tweenservice:Create(doorRight,tweeninginfo,propRight)
local tweenTwo = tweenservice:Create(doorLeft,tweeninginfo,propLeft)

local db = false
guiButton.MouseButton1Down:Connect(function()
if db == false then
db = true
tween:Play()
tweenTwo:Play()
wait(8.5)
db = false
end
end)

Oh yeah, remember to tween your union!

EDIT2: kingerman’s way might be more reliable.

Would it work if I put the union inside the part?

Thank you so much for helping me! One quick question, how do I make it slide out less?

Sorry for the delayed response
Change the 5 value in

for i=1,5,StudsPer do 

to whatever number you want

Thank you so much! You’re the best!