If true then; Expected Identifier

I want to have my script recognize when the ‘TV is ON’ so when clicked again it will do the opposite, ‘Turn OFF’.

Part of the script that isn’t working…

function onMouseClick()
if tvScreen.SurfaceGui.VideoFrame.Visible = true then
	powerButton.BrickColor = BrickColor.new('Really red')
	tvScreen.SurfaceGui.VideoFrame.Visible = false
	print("TV OFF")
end

I know it’s the ‘true then’ part which obviously isn’t correct and I can’t find what I need to do to fix this issue. I don’t normally do scripting.

Entire Script
local clickDetector = script.Parent
local powerButton = script.Parent.Parent
local tvScreen = script.Parent.Parent.Parent.Screen

function onMouseClick()
	if tvScreen.SurfaceGui.VideoFrame.Visible = true then
		powerButton.BrickColor = BrickColor.new('Really red')
		tvScreen.SurfaceGui.VideoFrame.Visible = false
		print("TV OFF")
	end
	powerButton.BrickColor = BrickColor.new('Lime green')
	tvScreen.SurfaceGui.VideoFrame.Visible = true
	print("TV ON")
end

clickDetector.MouseClick:connect(onMouseClick)

== not = when comparing as = is for assigning something to a variable, whilst == is used for comparision, also you can just write it as

if tvScreen.SurfaceGui.VideoFrame.Visible then

And for your case of toggling, I’d change it to use the and or fake tenary

function onMouseClick()
	local visible = tvScreen.SurfaceGui.VideoFrame.Visible
	tvScreen.SurfaceGui.VideoFrame.Visible = not visible
	powerButton.BrickColor = BrickColor.new(visible and 'Really red' or 'Lime green')
	print("TV " .. (visible and "OFF" or "ON"))
end

First line gets the visible property, the 2nd line inverts it and assigns it to the visible property, then the 3rd and 4th line use visible and (thing) or (thing), if visible is truthy then it will use the thing after and, if it is falsely, it will use the thing after or

2 Likes

Thank you, extremely helpful and even foreseen the loop issue.

1 Like