Trying to open & close a door with two buttons

Hello gamers, I’ve been sitting on this problem for a couple hours now; I’m creating (another) door. Everything works fine, however the problem arises when I try to have two buttons control the door. I use a local variable named “status” to signify if the door is open or closed in both scripts, so when it’s open for one, it’s closed for the other.

I’ve tried to just change it to a global variable and tried making the scripts fire a remote event so only one script opens the door, however I haven’t been able to get those to work.

Both buttons have the same exact script in them, which is shown below.

local TweenService = game:GetService("TweenService")
local status = "Closed"
local ClickDetector = script.Parent.ClickDetector

local d1i = script.Parent.Parent.D1.I
local d1o = script.Parent.Parent.D1.O
local d2i = script.Parent.Parent.D2.I
local d2o = script.Parent.Parent.D2.O

local d1ip = d1i.Position
local d1op = d1o.Position
local d2ip = d2i.Position
local d2op = d2o.Position

local TweenInfo = TweenInfo.new(
	2,
	Enum.EasingStyle.Quad,
	Enum.EasingDirection.InOut,
	0,
	false,
	0
)



ClickDetector.MouseClick:Connect(function(open)
	if status == "Closed" then
		TweenService:Create(d1i, TweenInfo, {Position=d1ip + Vector3.new(3.3,0,0)})  :Play()
		TweenService:Create(d1o, TweenInfo, {Position=d1op + Vector3.new(1.7,0,0)})  :Play()
		TweenService:Create(d2i, TweenInfo, {Position=d2ip + Vector3.new(-3.3,0,0)})  :Play()
		TweenService:Create(d2o, TweenInfo, {Position=d2op + Vector3.new(-1.7,0,0)})  :Play()
		status = "Open"
	else
		TweenService:Create(d1i, TweenInfo, {Position = d1ip}):Play()
		TweenService:Create(d1o, TweenInfo, {Position = d1op}):Play()
		TweenService:Create(d2i, TweenInfo, {Position = d2ip}):Play()
		TweenService:Create(d2o, TweenInfo, {Position = d2op}):Play()
		status = "Closed"
	end
	wait(1.5)
end)

two options I could think of off the top of my head would be either to put this all in one script, or use the global table _G and put the variable in there.

for example:

_G.status = 'Closed'

-- the rest of the code
if _G.status == 'Closed' then 
    --you get the point
end
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.