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
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)
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)
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
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)
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