How can I use a bool value in this situation?

So I have a door script that when the player clicks a button, the door closes and when the players clicks it again, it opens. What I want to do is to connect 2 bool values to the door opening and to the door closing and then use them to another script. I want to make it so when the door opens, then the power is draining but when it closes, the power stops draining.
I’m into scripting for some weeks and I don’t know how to make this yet. I don’t want people to make scripts for me, I just want someone to explain to me what to do and how I can start.
I tried looking for solutions for bool values in the devforum but couldn’t find anything. I tried experimenting my self but nothing worked.
Here is the code for the door

local Door = game.Workspace.DoorEngine.Door
local ClickDet = game.Workspace.DoorEngine.Button.ClickDetector

ClickDet.MouseClick:Connect(function()
	if not DoorOpen then DoorOpen = true
		if not DoorClose then DoorClose = true
			for i = 0,15 do
				Door.CFrame = Door.CFrame - Vector3.new(0,0.548,0)
				wait()
			end
			DoorOpen = false
			elseif DoorClose then DoorClose = false
				for i = 0,15 do
					Door.CFrame = Door.CFrame + Vector3.new(0,0.548,0)
					wait()
				end
			DoorOpen = false
			end
		end	
	end)
1 Like

In that case, you can use just a single variable or BoolValue object to track the door’s state. E.g. add a BoolValue to the door and call it “IsOpen”. Having a single place to store this information simplifies things while still allowing you to implement the desired behavior in the door.

You’ll want to reference IsOpen in the script. I’d make a change to how you’re referencing objects that are children of the door though. Your current approach takes an “absolute” path from the top of the instance hierarchy, with the built-in assumptions that the door exists, it’s in the right place and has the right name, and all the same assumptions for the children that must be in the door for it to work! If you instead use a “relative” path, i.e. starting inside the door, then it simplifies things and improves encapsulation, i.e. makes your whole door system more self contained. Here’s how that’d look:

local Door = script.Parent --This means the script must be inside the Door!
local ClickDet = Door.Button.ClickDetector --Relative path is shorter, and works even if you move the Door itself around!
local IsOpen = Door.IsOpen

I’d also get rid of the DoorOpen and DoorClose variables, because they don’t tell you anything that you can’t already figure out from the “IsOpen” BoolValue object.

Setting a property on the BoolValue object nicely allows you to do something in another script whenever the door opens or closes, e.g. starting and stopping the power drain. Here’s how that might look:

--Get a signal that fires when the IsOpen.Value changes, then connect a function to that signal that updates the power drain
Door.IsOpen:GetPropertyChangedSignal("Value"):Connect(function()
    if Door.IsOpen.Value then
        powerDrain = powerDrain + DOOR_POWER_DRAIN
    else
        powerDrain = powerDrain - DOOR_POWER_DRAIN
    end
end)

Hope this helps! You can call GetPropertyChangedSignal on any built-in object to make your code “react” to changes. It works for almost all properties, but double check that it works on Part.CFrame and Part.Position, I don’t know for sure but I think those might not work.

2 Likes

Sorry for not answering, I’m a little busy right now but I’ll try it in some minutes, thank you so much for helping me!

Hello, I can’t seem to find where I should put the IsOpen bool value inside the script, here is what I’ve done so far

local Door = script.Parent
local ClickDet = Door.Button.ClickDetector
local IsOpen = Door.IsOpen

IsOpen = true

ClickDet.MouseClick:Connect(function()
	if not IsOpen then
		IsOpen = false
			for i = 0,15 do
				Door.CFrame = Door.CFrame - Vector3.new(0,0.548,0)
			wait()
			end
			IsOpen = true
	if IsOpen then
		IsOpen = true
				for i = 0,15 do
					Door.CFrame = Door.CFrame + Vector3.new(0,0.548,0)
				wait()
				end
				IsOpen = true
				end
				end
	end)

You don’t need the additional IsOpen variabl at line 5.

local IsOpen = Door.IsOpen
IsOpen = true

This just overwrites the IsOpen variable with a new value. It’s supposed to be a reference to an object, like it is after running the first assignment, but the second assignment then sets it to a boolean value (that’s not the same thing as a BoolValue object).

Oh alright thanks I’ll change that but now I don’t know how I can make the door work again since “DoorOpen” and “DoorClose” variables are gone.