How to always check the boolvalue whenever it's true or false

I want to make a door. When boolvalue name “AllowOpen” is true, the door opens when you touch it.
The problem is I tried to set boolvalue from true to false. Which activate the neons color. And it keeps getting the same result.

ServerScripts:

local tweenservice = game:GetService("TweenService")
local runservice = game:GetService("RunService")

local info = TweenInfo.new(1, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out)

local touched = false
local AllowOpen = script.Parent.AllowOpen

local HeartBeat

HeartBeat = runservice.Heartbeat:Connect(function ()
	for i, door in pairs(script.Parent.Parent.D1:GetDescendants()) do
		if door:IsA("Part") and door.Name == "NeonActivator" then
			if not touched then
				if AllowOpen.Value == true then
					local t = tweenservice:Create(door, TweenInfo.new(.5), {Color = Color3.fromRGB(116, 166, 118)})

					t:Play()
				else
					local f = tweenservice:Create(door, TweenInfo.new(.5), {Color = Color3.fromRGB(166, 166, 166)})

					f:Play()
				end

			end
		end
	end
end)

local function Open_door(hit)
	for i, door in pairs(script.Parent.Parent.D1:GetDescendants()) do
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)

		if player and AllowOpen.Value == true then

			HeartBeat:Disconnect()
			script.Parent.CanTouch = false

			if door:IsA("Part") then
				local opentween = tweenservice:Create(door, info, {Position = door.Position + Vector3.new(0,10,0)})

				opentween:Play()
			end

			if door:IsA("Part") and door.Name == "NeonActivator" then
				local opentween = tweenservice:Create(door, TweenInfo.new(.5), {Color = Color3.fromRGB(206, 90, 90)})

				opentween:Play()
			end

		end
	end	
end





script.Parent.Touched:Connect(Open_door)

I tried using something like .Changed and GetPropertyChangedSignal() and I’m still confuse with those things. Could you give me an example or a short script that will make me understand easier?

Using a while loop (or in this case you’re using RunService.Heartbeat event) to achieve detect if something has changed can tank up performance (as a for loop is created and required to iterate).

And this is the exact reason why events exist. They only get fired when they detect a change.

In your case, since you’re using a BoolValue, you should use its Changed event, connect a function to it which, firstly, checks the new boolean value, then change the door’s colour based on the value using if statements.

1 Like

Thanks for the information. I’ll go think how to use by myself. Or maybe you can give me an example script.

I believe I have explained all the things you need to know. But whatever, here’s one example script.

BoolValue.Changed:Connect(function(newValue)
    if newValue then -- it's true.
        -- code here
    else -- it's false.
        -- code here
    end
end)

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