How to make TextLabel appear when ImageButton is Pressed, disappear after 3 seconds

  1. What do you want to achieve?
    I want to achieve a TextLabel that appears that says “Power = ON”, it disappears after 3 seconds. I then want it to say “Power = OFF” if it is pressed again.

  2. What is the issue?
    I cannot achieve it yet.
    image

  3. What solutions have you tried so far?
    I have looked around on the developer hub, and on the forums however I haven’t found anything.

Thanks.

2 Likes

Basic

local debris = game:GetService("DebrisService")

ImageButton.MouseButton1Click:Connect(function()
    debris:AddItem(MyText,3)
end)

--or
ImageButton.MouseButton1Click:Connect(function()
   task.wait(3)
   MyText:Destroy()
end)

Note, it’d only disappear for the local player.
ImageButton is the image button
MyText is the TextLabel

1 Like

DebrisService is not a valid service name

1 Like

Make a BoolValue in the PowerButton.
And Rename it to BoolValue
Use .MouseButton1Click event with the PowerButton.
You should put the code in a LocalScript within PowerButton.

When it is clicked, set the BoolValue.Value in PowerButton to “not” it’s value. Your code will look like this…

script.Parent.MouseButton1Click:Connect(function()
    script.Parent.BoolValue.Value = not script.Parent.BoolValue.Value -- Sets to NOT it's value. Basically the opposite.
   
end)

Next we make “if” statement to check IF the value is true or false.

script.Parent.MouseButton1Click:Connect(function()
    script.Parent.BoolValue.Value = not script.Parent.BoolValue.Value -- Sets to NOT it's value. Basically the opposite.

    if script.Parent.Parent.BoolValue.Value == false then

    else

    end
end)

Now, click on PowerMode, go to its properties and disable the property, “Visible”.
After that, we have to make the PowerMode visible for a short time and set its text too.

Next we shall add the code that will do what I said above.
We will make the PowerMode visible for a short amount of time and also set the text.

script.Parent.MouseButton1Click:Connect(function()
    script.Parent.BoolValue.Value = not script.Parent.BoolValue.Value -- Sets to NOT it's value. Basically the opposite.

    if script.Parent.BoolValue.Value == false then
        script.Parent.PowerMode.Text = "Power: OFF"

        coroutine.wrap(function()
            script.Parent.PowerMode.Visible = true
            task.wait(3)
            script.Parent.PowerMode.Visible = false
        end)
    else
        script.Parent.PowerMode.Text = "Power: ON"

        coroutine.wrap(function()
            script.Parent.PowerMode.Visible = true
            task.wait(3)
            script.Parent.PowerMode.Visible = false
        end)
    end
end)

Let me know if any errors get arised. I will fix the code.

1 Like

image
image

1 Like

Rename the Value to BoolValue. This should fix.

1 Like

Nothings happening. No UI appearing, nothing.

1 Like

No errors? Maybe if I change the code to this…

script.Parent.MouseButton1Click:Connect(function()
    if script.Parent.BoolValue.Value == false then
        script.Parent.BoolValue.Value =  true
        script.Parent.PowerMode.Text = "Power: OFF"

        coroutine.wrap(function()
            script.Parent.PowerMode.Visible = true
            task.wait(3)
            script.Parent.PowerMode.Visible = false
        end)
    else
        script.Parent.PowerMode.Text = "Power: ON"
        script.Parent.BoolValue.Value =  false

        coroutine.wrap(function()
            script.Parent.PowerMode.Visible = true
            task.wait(3)
            script.Parent.PowerMode.Visible = false
        end)
    end
end)

Nothing, no errors, nothing; F9 says nothing, UI dosent appear

I prefer using IntValue’s over bool values, so I’ll be explaning it using an IntValue.

Under “PowerMode” add an IntValue and name it “On”
Either use the local script under “PowerButton” or insert a new one.
Inside it put:

local on = script.Parent.PowerMode.On
script.Parent.MouseButton1Click:Connect(function()
if on.Value == 0 then
on.Value = 1
script.Parent.PowerMode.Visible = true
script.Parent.PowerMode.Text = "Power = ON"
wait(3)
script.Parent.PowerMode.Visible = false
return
end
if on.Value == 1 then
on.Value = 0
script.Parent.PowerMode.Visible = true
script.Parent.PowerMode.Text = "Power = OFF"
wait(3)
script.Parent.PowerMode.Visible = false
end
end)

OOOH my bad, i forgot to add a () at the end of end)
:grimacing:

script.Parent.MouseButton1Click:Connect(function()
    if script.Parent.BoolValue.Value == false then
        script.Parent.BoolValue.Value =  true
        script.Parent.PowerMode.Text = "Power: OFF"

        coroutine.wrap(function()
            script.Parent.PowerMode.Visible = true
            task.wait(3)
            script.Parent.PowerMode.Visible = false
        end)()
    else
        script.Parent.PowerMode.Text = "Power: ON"
        script.Parent.BoolValue.Value =  false

        coroutine.wrap(function()
            script.Parent.PowerMode.Visible = true
            task.wait(3)
            script.Parent.PowerMode.Visible = false
        end)()
    end
end)

both of these work. thank you!!

1 Like