Search code examples
luaroblox

How do I open a frame in my screengui from pressing a button on my surfacegui?


I am coding a computer in roblox studio where on the monitor there is a login button and I want it where when a player hit that button a frame from my screengui. Ive been trying different codes but none worked. Mt recent script was a script. (I tried both local and normal script of this code both didnt work.) this script is in the button of the surfacegui.

local MyButton = script.Parent
local frame = game.StarterGui.ScreenGui.Frame

MyButton.MouseButton1Click:Connect(function()
    frame.Visible = true
end)

![the hierarchy of the StarterGui.] (https://i.sstatic.net/TMWlbZBJ.png)

I tried making the code where when button is pressed it would make frame visible.


Solution

  • You have 2 main problems here, 1 is the fact that you are listening to a GuiButton on the server and that you are changing StarterGui.

    Instead of having a ServerScript listening to the events, you should use LocalScript, put the SurfaceGui in a ScreenGui and set the SurfaceGui's Adornee to the part.

    From there you can listen to the event from a LocalScript. Something like the below.

    local SurfaceGui = script.Parent.SurfaceGui
    local ScreenPart = workspace.Screen
    local frame = script.Parent.Frame
    
    SurfaceGui.Adornee = ScreenPart
    SurfaceGui.Button.MouseButton1Click:Connect(function()
        frame.Visible = true
    end)
    

    (where the LocalScript is in the root of the ScreenGui)