Function still running even if value is true

I have a door which should open only if a BoolValue in the door model is set to false, and should close if the value is true.

However, the open function still runs even if the value is already true.
It does this twice, then does the same thing with the close function.

if door.Open.Value then
				if doortype == "Swing" then
					ts:Create(door.DoorHinge, TweenInfo.new(doorconfig.Time), {CFrame = door.DoorHinge.CFrame * CFrame.Angles(0, 0, 0)}):Play()
				end
				door.Open.Value = false
			else
				if doortype == "Swing" then
					ts:Create(door.DoorHinge, TweenInfo.new(doorconfig.Time), {CFrame = door.DoorHinge.CFrame * CFrame.Angles(0, math.rad(90), 0)}):Play()
				end
				door.Open.Value = true
			end
3 Likes

You are simply checking if door.Open has a value on line 1.

if door.Open.Value == true then
    if doortype == "Swing" then
        ts:Create(door.DoorHinge, TweenInfo.new(doorconfig.Time), {CFrame = door.DoorHinge.CFrame * CFrame.Angles(0, 0, 0)}):Play()
    end
    door.Open.Value = false
else
    ts:Create(door.DoorHinge, TweenInfo.new(doorconfig.Time), {CFrame = door.DoorHinge.CFrame * CFrame.Angles(0, math.rad(90), 0)}):Play()
    door.Open.Value = true
end

This does not work. It does the same thing it did before. It should still do it without the == true though.

Do you mind sharing the rest of the code?

rs.InteractDoor.OnServerEvent:Connect(function(plr, door)
	print(plr.Name, door.Name)

	local card = plr.Backpack:FindFirstChild("Access Card") or plr.Character:FindFirstChild("Access Card")
	local doorconfig = require(door:FindFirstChild("DoorConfig"))
	local clearancegranted = false
	local teamgranted = false
	local usergranted = false
	local doortype = doorconfig.DoorType

	local function interactdoor(granted)
		if granted then
			door.DoorFrame.Accept:Play()
			for i,light in pairs(door:GetDescendants()) do
				if light.Name == "ScannerLight" then
					coroutine.wrap(function()
						light.Color = scannercolors.granted
						wait(0.5)
						if not door.Locked.Value then
							light.Color = scannercolors.default
						else
							light.Color = scannercolors.locked
						end
					end)()
				end
			end
			
			if door.Open.Value == true then
				if doortype == "Swing" then
					ts:Create(door.DoorHinge, TweenInfo.new(doorconfig.Time), {CFrame = door.DoorHinge.CFrame * CFrame.Angles(0, 0, 0)}):Play()
				end
				door.Open.Value = false
			else
				if doortype == "Swing" then
					ts:Create(door.DoorHinge, TweenInfo.new(doorconfig.Time), {CFrame = door.DoorHinge.CFrame * CFrame.Angles(0, 90, 0)}):Play()
				end
				door.Open.Value = true
			end
		else
			door.DoorFrame.Deny:Play()
			for i,light in pairs(door:GetDescendants()) do
				if light.Name == "ScannerLight" then
					light.Color = scannercolors.denied
					wait(0.5)
					if not door.Locked.Value then
						light.Color = scannercolors.default
					else
						light.Color = scannercolors.locked
					end
				end
			end
		end
	end
	
	local function clearancecheck(lockdown)
		clearancegranted = false
		teamgranted = false
		usergranted = false
		
		if not lockdown then
			for i, perms in pairs(doorconfig.Permissions) do
				print(i, perms)
				if i == "Clearance" then
					if card.Clearance.Value >= perms then
						clearancegranted = true
					else
						clearancegranted = false
					end
				end
				if i == "Teams" then
					for _, team in pairs(perms) do
						if plr.Team.Name == team then
							teamgranted = true
						else
							teamgranted = false
						end
					end
				end
				if i == "Users" then
					for _, user in pairs(perms) do
						if plr.UserId == user then
							usergranted = true
						else
							usergranted = false
						end
					end
				end
			end
		else
			for i, perms in pairs(doorconfig.LockdownPermissions) do
				print(i, perms)
				if i == "Clearance" then
					if card.Clearance.Value >= perms then
						clearancegranted = true
					else
						clearancegranted = false
					end
				end
				if i == "Teams" then
					for _, team in pairs(perms) do
						if plr.Team.Name == team then
							teamgranted = true
						else
							teamgranted = false
						end
					end
				end
				if i == "Users" then
					for _, user in pairs(perms) do
						if plr.UserId == user then
							usergranted = true
						else
							usergranted = false
						end
					end
				end
			end
		end
		
		if clearancegranted or teamgranted or usergranted then
			interactdoor(true)
		else
			interactdoor(false)
		end
		
	end

	if card then
		if not door.Locked.Value then
			clearancecheck(false)
		else
			clearancecheck(true)
		end
	end
end)

My recommendation would be to apply a debounce to your program to prevent it from firing multiple times. I don’t know if this’ll fix the problem, but it’ll improve one issue at least.

local cooldown = true
local waitingTime = 5

rs.InteractDoor.OnServerEvent:Connect(function(plr, door)
    if cooldown == false then
        return
    end
    cooldown = false
    delay(waitingTime, function()
        cooldown = true
    end)
    
    print(plr.Name, door.Name)

	local card = plr.Backpack:FindFirstChild("Access Card") or plr.Character:FindFirstChild("Access Card")
	local doorconfig = require(door:FindFirstChild("DoorConfig"))
	local clearancegranted = false
	local teamgranted = false
	local usergranted = false
	local doortype = doorconfig.DoorType
    ...
1 Like

In the if statement you need perms.Value or user.Value if the folder contains value instances like intValue. I am just guessing about that.

if plr.UserId == user.Value then

If that does not work, try using the debugger to make a breakpoint at the start then step through the code, and use the “watch” window to see what the variables are.

1 Like

doorconfig.Permissions is in a ModuleScript.
image

Did applying a debounce help any at all?

1 Like

In that case I don’t know why it misbehaves. Interesting way to use module scripts, I’ll have to try that.

Stepping through the code in the debugger is my best advice, in that case.

1 Like

Not really, it just made how it behaves weirder.

In my opinion, I think one of the biggest problems is the fact that all your functions are inside the scope of the RemoteEvent. I recommend moving them outside and modifying them to reflect that change.

I have a feeling it could have something to do with this.
image
It might be the reason why it’s running the function more than once.

1 Like

That’s why I asked about the debounce. You said earlier that it only made it behave weirder, but how?

1 Like

Actually wait, when I applied the debounce, I added a thing where it beeps red if you try interacting with it while the debounce is active, and it did infact beep red even when the door was opening. So I think that it could be that line is causing the interactdoor function to run more than once.

1 Like

Honestly I would recommend re-writing your program at this point. It’s very confusing to read and could perform better.

1 Like

I’m not really that good, but I’ll try.

1 Like

I would get you to a starting place, but I no longer develop on the Roblox platform and hadn’t for over a year. I’m basically here to make some replies and help some users while I’m at it.

1 Like

I ended up opening notepad and did some work on it. Lmk how this looks to you.

local door = ... -- do not let this be client-sided
local doorconfig = require(door:FindFirstChild("DoorConfig"))
local doortype = doorconfig.DoorType
local lockdown = door.Locked
local originalHingeAngle = door.DoorHinge.CFrame
local cooldown = true
local waitTime = 2


local function PlayerGrantedAccess(plr, card, permissions)
	for i, perms in pairs(permissions) do
		if i == "Clearance" and card ~= nil and card.Clearance.Value >= perms then
			return true
		elseif i == "Teams" or i == "Users" then
			for _, val in pairs(perms) do
				if plr.Team.Name == val or plr.UserId == val then
					return true
				end
			end
		end
	end
	
	return false
end

local function InteractDoor(granted)
	if granted then
		door.DoorFrame.Accept:Play()
		for _, light in pairs(door:GetDescendants()) do
			if light.Name == "ScannerLight" then
				light.Color = scannercolors.granted
				delay(0.5, function()
					if door.Locked.Value == false then
						light.Color = scannercolors.default
					else
						light.Color = scannercolors.locked
					end
				end)
			end
		end
		
		if door.Open.Value == true then
			if doortype == "Swing" then
				ts:Create(door.DoorHinge, TweenInfo.new(doorconfig.Time), {CFrame = originalHingeAngle}):Play()
			end
			door.Open.Value = false
		else
			if doortype == "Swing" then
				ts:Create(door.DoorHinge, TweenInfo.new(doorconfig.Time), {CFrame = originalHingeAngle * CFrame.Angles(0, math.rad(90), 0)}):Play()
			end
			door.Open.Value = true
		end
	else
		door.DoorFrame.Deny:Play()
		for _, light in pairs(door:GetDescendants()) do
			if light.Name == "ScannerLight" then
				light.Color = scannercolors.denied
				delay(0.5, function()
					if door.Locked.Value == false then
						light.Color = scannercolors.default
					else
						light.Color = scannercolors.locked
					end
				end)
			end
		end
	end
end

rs.InteractDoor.OnServerEvent:Connect(function(plr)
	if cooldown == false then
		return
	end
	cooldown = false

	local card = plr.Backpack:FindFirstChild("Access Card") or plr.Character:FindFirstChild("Access Card")
	local permissionsToUse = lockdown.Value == true and doorconfig.LockdownPermissions or doorconfig.Permissions
	
	local playerCanEnter = PlayerGrantedAccess(plr, card, permissionsToUse)
	
	InteractDoor(playerCanEnter)

	wait(waitTime)
	cooldown = true
end)