How can i allow multiple people to view a gui?

Ive tried doing stuff like putting “KingJesseDev” or “AndroidYTX3”, and ive tried doing and, but they lead to either only showing the gui for me or showing it for everyone. I know you can duplicate the gui and change the username to “AndroidYTX3”, but that feels abit slower when you update the gui and having to do that each time. Here’s my code:

if game.Players.LocalPlayer.Name == "KingJesseDev" or "AndroidYTX3" then
wait()
else
script.Parent:Destroy()
end
1 Like

Your issue with this script is you have to state you’re comparing the string "AndroidYTX3" to game.Players.LocalPlayer.Name, which you did with "KingJesseDev".

It would look something like this

if game.Players.LocalPlayer.Name == "KingJesseDev" or game.Players.LocalPlayer.Name =="AndroidYTX3" then
    wait()
else
    script.Parent:Destroy()
end

You could simplify this with

local Name = game.Players.LocalPlayer.Name
if Name == "KingJesseDev" or Name =="AndroidYTX3" then
    wait()
else
    script.Parent:Destroy()
end

It might seem unnecessary to put it twice, and believe me when I began scripting I agreed, but that’s how you can do this with lua. Hope this helps :slight_smile:

(Also a better way might be with UserIds, just in case one of you changes your username, you won’t have to update the script every time.)

3 Likes

Testing this out, marking as a solution if this works!

Works! Thank you for your help!

Although this is already answered and marked as a solution, I’ll give a different approach and more flexible script.

Check as Username:

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer

local allowedPlayers = {"Roblox", "KingJesseDev", "AndroidYTX3"}

local function checkAllowed()
    for _, thisPlayer in pairs(allowedPlayers) do
        if thisPlayer == LocalPlayer.Name then
            return true
        end
    end
end

if checkAllowed() then --// This will return if TRUE or FALSE
    -- IF TRUE
    wait()
else
    -- IF FALSE
    script.Parent:Destroy()
end

Check as UserId:

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer

local allowedPlayers = {1, 1295781757, 1056976476}

local function checkAllowed()
    for _, thisPlayer in pairs(allowedPlayers) do
        if thisPlayer == LocalPlayer.UserId then
            return true
        end
    end
end

if checkAllowed() then --// This will return if TRUE or FALSE
    -- IF TRUE
    wait()
else
    -- IF FALSE
    script.Parent:Destroy()
end

I recommend that you use UserId when checking because they might change their Usernames and your allowedPlayers will not work for the listed value inside because they have a different username from the recorded.

2 Likes

Glad you included UserId, it’s crazy how often this gets overlooked.

1 Like

UserIds is a much better idea (as I noted) and yes @OP this approach is significantly better if you plan on adding more people, an elongated if statement would be extremely tedious compared to a table of names. :slight_smile:

3 Likes