Search code examples
luaroblox

Im trying to make a Roblox GUI that changes a text but its not working


game.StarterGui.ScreenGui.TextButton.MouseButton1Click:Connect(function()       
        game.StarterGui.ScreenGui.TextButton.Text = ("Clicked")
        wait(1)
        game.StarterGui.ScreenGui.TextButton.Text = ("CLICK ME.")
end)

I'm new to programming in Roblox Studio so I am probably doing a simple mistake.


Solution

  • Your issue here is you are using a script underneath Workspace instead of under StarterGui.

    You will notice when you test a game that all of the items under StarterGui get moved under the Player objects in "Players". You need to move move this script inside of the ScreenGui and reference it as follows:

    -- Parent object
    local screenUI = script.Parent
    
    screenUI.TextButton.MouseButton1Click:Connect(function()       
        screenUI.TextButton.Text = ("Clicked")
        wait(1)
        screenUI.TextButton.Text = ("CLICK ME.")
    end)