TV Script not working

So I’m trying to make a TV that used buttons to turn on and off. I’m trying to make it choose 1 or 4 image to show after its turned on. I have it working but it only shows the 1st image

This is what happens every time I press the button

What i have is this

local clickDetector = game.Workspace.TVTable2.DVD.On.ClickDetector
local screen = game.Workspace.TVTable2.TV.Screen
local show1 = game.Workspace.TVTable2.TV.Screen.Show1
local show2 = game.Workspace.TVTable2.TV.Screen.Show2
local show3 = game.Workspace.TVTable2.TV.Screen.Show3
local show4 = game.Workspace.TVTable2.TV.Screen.Show4

function onMouseClick()
	math.random(1,4)
	wait(3)
	if math.random (1) then
		show1.Transparency = 0
		print("Show 1")
	else
		if math.random (2) then
			show2.Transparency = 0
			print("Show 2")
	   else
			if math.random (3) then
				show3.Transparency = 0
				print("Show 3")
			else
				if math.random (4) then
					show4.Transparency = 0
					print("Show 4")
				end
			end
		end
	end
end

clickDetector.MouseClick:connect(onMouseClick)

Help would be appreciated, Thanks!

Edit: I forgot to mention I am still a beginner

1 Like

The issue is that each conditional is evaluating a different number because the function is getting called each time. Try this:

local clickDetector = game.Workspace.TVTable2.DVD.On.ClickDetector
local screen = game.Workspace.TVTable2.TV.Screen
local shows = {
	game.Workspace.TVTable2.TV.Screen.Show1,
	game.Workspace.TVTable2.TV.Screen.Show2,
	game.Workspace.TVTable2.TV.Screen.Show3,
	game.Workspace.TVTable2.TV.Screen.Show4
}

function onMouseClick()
	local show_number = math.random(1,#shows)
	wait(3)
	for index,show in ipairs(shows) do
		if index == show_number then
			show.Transparency = 0
		else
			show.Transparency = 1
		end
	end
	print("Show "..show_number)
end

clickDetector.MouseClick:connect(onMouseClick)
1 Like

You’re using math.random wrong. You want to call math.random once, it will return the random number. Then you use that number it returned. Your conditional syntax is a little off as well.

local n = math.random(1,4)

if n == 1 then
    show1.Transparency = 0

elseif n == 2 then
    show2.Transparency = 0

elseif n == 3 then
    show3.Transparency = 0

elseif n == 4 then
    show4.Transparency = 0
end
1 Like

assign a variable to math.random. Then check each number. also use elseif instead of else.

1 Like

set this to a local variable:

function onMouseClick()
	local rand = math.random(1,4)
	wait(3)
	if rand == 1 then
		show1.Transparency = 0
		print("Show 1")
	elseif rand == 2 then
			show2.Transparency = 0
			print("Show 2")
	   elseif rand == 3 then
				show3.Transparency = 0
				print("Show 3")
			elseif rand == 4 then
					show4.Transparency = 0
					print("Show 4")
	end
end
1 Like

Using a lookup table is a good practice but isn’t required in this situation. You can use FindFirstChild to directly find the proper instance.

function onMouseClick()
    local n = math.random(1,4)

    local thisShow = game.Workspace.TVTable2.TV.Screen:FindFirstChild('Show' .. n)

    if thisShow then
        thisShow.Transparency = 0
    end
end

clickDetector.MouseClick:Connect(onMouseClick)
1 Like

local clickDetector = game.Workspace.TVTable2.DVD.On.ClickDetector
local screen = game.Workspace.TVTable2.TV.Screen
local show1 = game.Workspace.TVTable2.TV.Screen.Show1
local show2 = game.Workspace.TVTable2.TV.Screen.Show2
local show3 = game.Workspace.TVTable2.TV.Screen.Show3
local show4 = game.Workspace.TVTable2.TV.Screen.Show4

function onMouseClick()
local randomNumber = math.random(1,4)
wait(3)
if randomNumber (1) then
show1.Transparency = 0
print(“Show 1”)
else
if randomNumber (2) then
show2.Transparency = 0
print(“Show 2”)
else
if randomNumber (3) then
show3.Transparency = 0
print(“Show 3”)
else
if randomNumber (4) then
show4.Transparency = 0
print(“Show 4”)
end
end
end
end
end

clickDetector.MouseClick:connect(onMouseClick)

Sorry for not formatting the code, idk how to do it :confused:

also use elseif instead of else

1 Like

@MysteriousVagabond @InfinityDesign @Ae_xxie @WooleyWool @DevNoobxd
I just read all of these and am currently trying them. Thank you for the help!

1 Like

do ```lua, paste the code and do the same thing but without the lua

Yeah I know how to do it but I don’t have these: ``` on my keyboard. i only have this:´´