Script failing to set the value of a variable

Hello there you lot, I am trying to move a part to a different place, depending on a boolValue. And for some reason, it’s just not working. I know I can make a door not collide, but I need it to move.

Code here:

local Service = game:GetService("ProximityPromptService")
local Prompt = script.Parent
local mdl = script.Parent.Parent.Parent.Parent
local door = mdl.Door
local Close = mdl.Var.Closed
local posX = script.Parent.Parent.Parent.Parent.Door.Position.X
local posY = script.Parent.Parent.Parent.Parent.Door.Position.Y
local posZ = script.Parent.Parent.Parent.Parent.Door.Position.Z

local function OpenClose()
	if Close.Value == true then
		Close.Value = false
		---print(Close.Value)
		door.Position = Vector3.new(0, 0, 0)
	else if Close.Value == false then
			Close.Value = true
			---print(Close.Value)
			door.Position = Vector3.new(posX, posY, posZ)
		end
	end
end

Service.PromptTriggered:Connect(OpenClose)

It always outputs that the variable is true…

Any help is greatly appreciated!

1 Like

Can you tell me if the variable is currently false or true?

Before running the script it is set to false

After running the script and pressing the button multiple times it is true
Output:

  10:18:20.073  true (x4)  -  Server - Script:17
local Service = game:GetService("ProximityPromptService")
local Prompt = script.Parent
local mdl = script.Parent.Parent.Parent.Parent
local door = mdl.Door
local Close = mdl.Var.Closed
local posX = script.Parent.Parent.Parent.Parent.Door.Position.X
local posY = script.Parent.Parent.Parent.Parent.Door.Position.Y
local posZ = script.Parent.Parent.Parent.Parent.Door.Position.Z

local function OpenClose()
	if Close.Value == true then
		---print(Close.Value)
		door.Position = Vector3.new(0, 0, 0)

		Close.Value = false
	else if Close.Value == false then
			---print(Close.Value)
			door.Position = Vector3.new(posX, posY, posZ)
                        Close.Value = true
		end
	end
end

Service.PromptTriggered:Connect(OpenClose)

Try this, not sure if it will work but try it momentarily while I try to use my brain.

Now it always outputs false

Output:

  10:21:17.282  false (x6)  -  Server - Script:17

That won’t work since you’ve just moved the boolean value assignments to the end of the conditional bodies as opposed to the start of them.

local Service = game:GetService("ProximityPromptService")
local Prompt = script.Parent
local mdl = script.Parent.Parent.Parent.Parent
local door = mdl.Door
local Close = mdl.Var.Closed
local posX = script.Parent.Parent.Parent.Parent.Door.Position.X
local posY = script.Parent.Parent.Parent.Parent.Door.Position.Y
local posZ = script.Parent.Parent.Parent.Parent.Door.Position.Z

local function OpenClose()
	if Close.Value then
		Close.Value = false
		door.Position = Vector3.new(0, 0, 0)
	elseif not Close.Value then
		Close.Value = true
		door.Position = Vector3.new(posX, posY, posZ)
	end
end

Service.PromptTriggered:Connect(OpenClose)

You had a space between “else” and “if” likely unintentionally. I’ve also removed “== true” & “== false” since they’re both redundant.

Hmm, it still doesn’t work. I checked the value of the boolean again, and it’s still true and only moving the door to 0,0,0

Output

  10:30:30.744  true (x6)  -  Server - Script:17

If before the script is ran the BoolVal instance is set to false why is “true” being printed?

No idea man… Maybe there’s a better way to do the same thing I want to do?

Could you add a print(Close.Value) below the local Close line?
This will print whatever value is stored by the variable named “Close” directly after it has been declared.

image
Output:

  10:37:02.086  false  -  Server - Script:6

Okay, as expected. Was false then printed a further 6 times?

No, It prints true after the function is ran.

Where exactly is the print in the function, just to clarify?

local function OpenClose()
	if Close.Value then
		Close.Value = false
		print(Close.Value)
		door.Position = Vector3.new(0, 0, 0)
	elseif not Close.Value then
		Close.Value = true
		print(Close.Value)
		door.Position = Vector3.new(posX, posY, posZ)
	end
end

Each print function is below the set for Close.Value, so I would expect it to print true once, and then false.

function OpenClose()
	local isClosed = Close.Value
	if isClosed then
		Close.Value = false
		print(Close.Value)
		door.Position = Vector3.new(0, 0, 0)
	else
		Close.Value = true
		print(Close.Value)
		door.Position = Vector3.new(posX, posY, posZ)
	end
end

Still only outputs true. This is so weird

  10:48:22.685  true (x7)  -  Server - Script:19

Could you try testing this on a blank baseplate with the required instances? Broken scripts/plugins could be interfering with the script. Do you see any other errors in console regarding other game aspects?