Local scripts and Gui

Ahoy Everyone, here is a simple introduction to local scripts and Gui.

Local scripts are some of the most important things for scripting. Local scripts are scripts that only appear on the client there are 2 sides of Roblox the client and the server. The client is the players view so stuff like GUI. to create a local script there are a lot of way’s to put them. Here are the places
image
this is starter gui this is where you create gui and all of that. To insert a local script click the plus icon for starter gui then click “local script” instead of the normal “script” now that we have our first local script lets start coding in it.

local player = game.Players.LocalPlayer

what this will do is it will create a variable that is a player or also known as the player’s client. There, lets create a screen Gui now. Click the plus button on the starter GUI and click screen Gui. Now we have our first Gui, let’s create a text button click the plus button on your screen Gui then click text button. You can customize your button so lets do it.

Double click the text button so you can change your text you can also rename your text button if you would like, I named mine “clickButton” now lets create a frame you just insert a frame instead of a text button. Now lets make your frame not visible go in your properties scroll down a little and un check the visible property. Now we should see our frame is not visible anymore.

Now create a local script inside the click button. Lets write a script.

local clickButton = script.Parent

local Frame = clickButton.Parent.Frame

clickButton.MouseButton1Click:Connect(function() --mouse click event
	  --code goes here
end)

Now we used a event when the player clicks the button, lets make it so that our frame is visible when we click our button. So, here’s are new script

local clickButton = script.Parent

local Frame = clickButton.Parent.Frame

clickButton.MouseButton1Click:Connect(function() --mouse click event
	Frame.Visible = not Frame.Visible
end)

what this local script will do is when you click the button it makes it visible again and then if you click it again then it goes back to not being visible again. Now that we’ve done a introduction to local scripts and Gui I hope you understand what they are.

You could reply and tell me what to fix on this post if I messed up anything this may not be the most in depth tutorial and local scripts and Gui.

Thanks for reading and goodbye.

4 Likes

Using local scripts inside of StarterGui isn’t good.
Mainly because each script will be re-ran basically when the player respawns.

You can use a script inside of StarterPlayerScripts to access their UI.

Personally I use a module to help look for instances easily.

Telescope module:

--!strict

local Telescope = {}

function Telescope.Find(location: Instance, target: string): Instance?
    for _, item in location:GetDescendants() do
        if item.Name == target then
            return item
        end
    end
    
    return nil
end

function Telescope.FindAll(location: Instance, target: string)
    local results = {}

    for _, item in location:GetDescendants() do
        if item.Name == target then
            table.insert(results, item)
        end
    end

    return results
end

function Telescope.FindClass(location: Instance, class: string): Instance?
    for _, item in location:GetDescendants() do
        if item:IsA(class) then
            return item
        end
    end
    
    return nil
end

function Telescope.FindClassAll(location: Instance, class: string): {Instance}
    local results = {}

    for _, item in location:GetDescendants() do
        if item:IsA(class) then
            table.insert(results, item)
        end
    end

    return results
end

function Telescope.Path(location: Instance, path: string)
    local path = path:split("/")
    local instance = location
    
    for i = 1, #path do
        instance = instance:FindFirstChild(path[i])
    end
    
    return instance
end

function Telescope.PathAwait(location: Instance, path: string, time_out: number?)
    local path = path:split("/")
    local instance = location

    for i = 1, #path do
        instance = instance:WaitForChild(path[i], time_out or 5)
    end

    return instance
end

return Telescope

Example:

local Players = game:GetService('Players')
local Player = Players.LocalPlayer
local PlayerGui = Player:WaitForChild('PlayerGui')

local Telescope = require('@self/Telescope') --> telescope module

local Button = Telescope.PathAwait(PlayerGui, 'ScreenGui/TextButton', 5) :: TextButton

Button.Activated:Connect(function()
    print('Button Activated')
end)

thanks for showing me, this I thought starter gui would be more simple

that’s not always the case as you can disable ResetOnSpawn on the screen gui
(also your FindClass function could be replaced simply with location:FindFirstChildWhichIsA(class, true), but that’s not related to this thread)