Door not opening after a value is true?

I tried making an egg game when you click the egg it changes a value to true, which opens a door. But it doesn’t work with no errors. I tried searching it up, but no luck.
Code of the door.

local WaterEggCollected = game.Workspace.Values.WaterEggCollected.Value
local Door1 = game.Workspace.Door1
while true do
	if WaterEggCollected == true then
		Door1.Transparency = 1
		Door1.CanCollide = false
	end
end

Code of the egg.

function OnClicked (mouse)
	script.Parent:Destroy()
	game.Workspace.Values.WaterEggCollected.Value = true
end
script.Parent.ClickDetector.MouseClick:connect(OnClicked)

EDIT: It actually showed the error" [17:46:09.222 - Script timeout: exhausted allowed execution time]"

Add a wait() in the while true do loop

local WaterEggCollected = game.Workspace.Values.WaterEggCollected.Value
local Door1 = game.Workspace.Door1
while true do
       wait()
	if WaterEggCollected == true then
		Door1.Transparency = 1
		Door1.CanCollide = false
	end
end
2 Likes

Thank you, but, it still doesn’t do anything when I add " wait()" to it.

1 Like

Are there any errors after that? if not, try checking the distance of the click detector.

1 Like

There are no errors after that, and the distance of the click detector is 10.

1 Like

The script is removed before it finished executing this line

game.Workspace.Values.WaterEggCollected.Value = true

Just switch them

game.Workspace.Values.WaterEggCollected.Value = true
script.Parent:Destroy()

It’s also bad practice to use a while loop to constanly check if a bool object property changed. Use .Changed instead.

2 Likes

If what starmaq said doesn’t work, then try making all the scripts server scripts. Cause the script that sets the value could be a local script and it might not replicate.

1 Like

Ok, I also changed the code a little so instead of deleting it, it does

game.Workspace.Values.WaterEggCollected.Value = true
	script.Parent.Transparency = 1
	script.Parent.CanCollide = 1

Move .Value and place it in the if statement,

If WaterEggCollected.Value == true then

In the door script

1 Like

I did use server scripts, I know the egg click script works, because it changes the value of the boolvalue “WaterEggCollected”, so I don’t know why it is not working in the door script.

1 Like

Use this instead, I think this would help:

WaterEggCollected.Changed:Connect(function()
--Code (if water true then)
end)
2 Likes

I got the error " 17:59:37.531 - Workspace.Door1.Script:3: attempt to index boolean with ‘Changed’" when I did that

You could change your value from bool (true/false) to int/number. It’s much more easier.

1 Like

You should put the part of the script that changes the door’s properties and put it into the Click Detector script:

local Door1 = game.Workspace.Door1

function OnClicked (mouse)
	Door1.Transparency = 1
	Door1.CanCollide = false
	game.Workspace.Values.WaterEggCollected.Value = true
	script.Parent:Destroy()

end
script.Parent.ClickDetector.MouseClick:connect(OnClicked)
1 Like

Ignore what I said, use this:

local WaterEggCollected = game.Workspace.Values.WaterEggCollected
local Door1 = game.Workspace.Door1
WaterEggCollected.Changed:Connect(function()
	if WaterEggCollected.Value then
		Door1.Transparency = 1
		Door1.CanCollide = false
	end
end)
1 Like

Thank you! I just realized that I could just open the door using the same script

Sorry for the nitpick, but the “mouse” in function OnClicked (mouse) isn’t actually the mouse, its the player who clicked.

1 Like