TextLabel not going visible by script

I am trying to make a restocking system. So when all the cup’s transparency is 1, a Textlabel would turn visible and saying, “Restock now!”. The script is not working for some reason, how would I fix this?

Script:

local cup1 = game.Workspace.CupGiver.cup1
local cup2 = game.Workspace.CupGiver2.cup2
local cup3 = game.Workspace.CupGiver3.cup3
local cup4 = game.Workspace.CupGiver4.cup4
local Label = game.Workspace.Labels.BillboardGui.Label

if cup1.Transparency == 1 and cup2.Transparency == 1 and cup3.Transparency == 1 and cup4.Transparency == 1 then
	Label.Visible = true
	print("Cups need to be retocked!")
end

When is the cup’s transparency being reset? The script is not functioning to your purpose because it runs before the cups become transparent.

In this case, it’s always helpful to use Changed functions, as they detect when an object’s property changes, then the script will run.

For example:

cup1.Changed:Connect(function()
--Code here
end)

Of course, you have so many cups though, I would do a recursive loop and do a changed function within that.

I would place all the cups in a Model and then define their presence. So:

for _, v in pairs(cupsModel:GetChildren()) do
    v.Changed:Connect(function()
        if v.Transparency == 1 then
            Label.Visible = true
            print("Cups need to be restocked!")
        end
    end
end

Here’s the documentation if you would like to learn more about Changed functions.

Edit: you can also create a while true do loop for this (instead of a function), but I wouldn’t rely on using a while true do loop to fix your problems too much.

1 Like

Is this in a loop?, If not it will check only once if it is transparent or not.

local cup1 = game.Workspace.CupGiver.cup1
local cup2 = game.Workspace.CupGiver2.cup2
local cup3 = game.Workspace.CupGiver3.cup3
local cup4 = game.Workspace.CupGiver4.cup4
local Label = game.Workspace.Labels.BillboardGui.Label

while wait(.5) do
if cup1.Transparency == 1 and cup2.Transparency == 1 and cup3.Transparency == 1 and cup4.Transparency == 1 then
	Label.Visible = true
	print("Cups need to be retocked!")
end 
end

The if statement will only run once, unless you put it inside a loop or function.

3 Likes

I just want to add on to @Clueless_Brick’s post

Cup1.Changed:Connect(function()
if Cup1.Transparency == 1 and
Cup2.Transparency == 1 and Cup3.Transparency == 1 and Cup4.Transparency == 1 then
-- what you wanna do if transparency is set to 1.
end
end)
Cup2.Changed:Connect(function()
if Cup1.Transparency == 1 and
Cup2.Transparency == 1 and Cup3.Transparency == 1 and Cup4.Transparency == 1 then
-- what you wanna do if transparency is set to 1.
end
end)
Cup3.Changed:Connect(function()
if Cup1.Transparency == 1 and
Cup2.Transparency == 1 and Cup3.Transparency == 1 and Cup4.Transparency == 1 then
-- what you wanna do if transparency is set to 1.
end
end)
Cup4.Changed:Connect(function()

if Cup1.Transparency == 1 and
Cup2.Transparency == 1 and Cup3.Transparency == 1 and Cup4.Transparency == 1 then
-- what you wanna do if transparency is set to 1.
end
end)

This will keep track when the value ever changes, if its 1 then the below if statement will fire.

Why are you making events inside of another event? That will cause performance issues.

1 Like

OH SORRY. I didnt realise that, its hard typing on phone, I try simplifying word to word as much as possible.

Instead of making events inside of events, you can do this:

local function x()
    print("Hello world")
end

instance1.Event:Connect(x)
instance2.Event:Connect(x)
-- and so on

Of course, this would not actually work whatsoever.

1 Like

Heres a simpler script that doesnt require a lot of .Transparency statements! :smiley:

Code

local allCups = path.to.where.all.the.cups.are -- Change this to the path of all the cups!
local label = path.to.where.you.put.the.textlabel -- Change this to the path of the text label too! :)
local cups = {}

for _, v in pairs(allCups:GetChildren()) do
    if string.find(v.Name:lower(), "cup") then
        table.insert(cups, v)
    end
end

while wait(1) do
    local transparentCups = 0
    for _, v in pairs(cups) do
        if v.Transparency == 1 then
            transparentCups += 1
        end
    end
    if transparentCups >= #cups then
        label.Visible = true -- Show Label
    else
        label.Visible = false -- Hide Label
    end
end

How does my script work?

It starts off by creating a table called cups and looping through every part inside the model of allCups and if it has the word "cup" in its name, it will add it to the table cups.

Then every 1 second, it loops through the cups table and checks if the indexed "cup" is completely transparent. If it is, it adds 1 to a variable called transparentCups.

After the loop, it checks if transparentCups is greater than or equal to the amount of objects in cups and if so, shows the label. If not, it hides the label!

Conclusion

So, thats my post! If you think it is easier to use, or something, feel free to change the answer.

Also, cool thing: You can add as many cups as you want and it will still count properly without any code modifying! It won’t count any new cups if it was added after the first loop was run, though. And, you don’t need to hide the text label yourself, it will do that automatically after at least one cup has no transparency again.

Make sure you change the allCups and label variables to the paths of where all the cups are and the TextLabel.

Have a good day!

1 Like