Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make a GUI only display to certain people?

Asked by 4 years ago
Edited 4 years ago

I have a speed button in my game, but I only want it to be only for specific people. My issue right now is that the button displays to everyone. Here's my code right now.

game.Players.PlayerAdded:Connect(function(plr)
    game.StarterGui.ScreenGui:WaitForChild("Button")
    if plr.UserId == 123 or plr.UserId == 321 then
        script.Parent.Visible = true
        script.Parent.Active = true
    else
        script.Parent.Visible = false
        script.Parent.Active = false
    end
end)

I have this script in the button I want to display, and it is a local script.

(Btw I'm inexperienced in Lua, I just started recently)

2 answers

Log in to vote
0
Answered by
Thetacah 712 Moderation Voter
4 years ago
Edited 4 years ago

The easiest way to accomplish this would be to insert the following code inside a LocalScript, which should be inside the button.

You shouid also set Visible = false manually by changing this in properties

local list = {35910290, 12421, 41221} --Here, we create a table which stores the UserIds of all users who you want the GUI to be visible for. It's much better to use UserId's over usernames since you can change usernames on ROBLOX!

local button = script.Parent --Here we create a variable "button", which refers to the script.Parent(which is the button you want to be visible). This can be a frame too.
local player = game.Players.LocalPlayer --Creating a player variable which refers to the LocalPlayer

for _, userId in pairs(list) do --Here we create a forloop where we will iterate through each item stored in the "list" table(UserIds) We will compare each userId with each value in the table to see if they match (This would mean that we need to make it visible for them).
    if (userId == player.UserId) then --If they are on the list, then
        button.Visible = true --Make the button visible
    end
end 


Cleaner version, without all the comments:

local list = {35910290, 12421, 41221} --userIDS

local button = script.Parent
local player = game.Players.LocalPlayer

for i, userId in pairs(list) do
    if (userId == player.UserId) then
        button.Visible = true
    end
end 


0
Huh. This doesn't work either. JoshGamingHQ1 43 — 4y
0
Yes it does. I tested it myself. Did you put it in a LocalScript, inside the GUI you want to be visible? Did you set the button to not visible in the property panel? You also need to make sure to add your userID in that table! Your userId = 171801589 Thetacah 712 — 4y
0
If you still can't get it to work, send me a picture of your Explorer so I can try and assist you further. Thetacah 712 — 4y
0
OH I have the GUI set to visible by default. Let me test that. JoshGamingHQ1 43 — 4y
0
Yep this works great! Thanks so much! JoshGamingHQ1 43 — 4y
Ad
Log in to vote
0
Answered by
uhSaxlra 181
4 years ago
Edited 4 years ago

I've been using the or function, and it doesn't work for me, so I used a list instead. ALSO: Your using the startergui, which is not located in the player. To do so, use playergui, as in the script.

local list = {"165", "132"} --User id's of wanted players
game.Players.PlayerAdded:Connect(function(plr)
    local passed = false
    plr.PlayerGui.ScreenGui:WaitForChild("Button") -- not practicall but okay,
    for i,v in pairs(list) do -- Loops through the list of Userids
     if plr.UserId == v then --  Checks if the player's UserId is equal to one in the list
          passed = true -- If so, they passed
     end
    end
    if passed == true then -- if they pass, they get the gui, if not, rip.
        script.Parent.Visible = true
            script.Parent.Active = true -- not needed
    else
            script.Parent.Visible = false
            script.Parent.Active = false -- Not needed
    end
end)
0
Ok. But where would I move the button to? I don't see a tab for PlayerGUI. JoshGamingHQ1 43 — 4y
0
This won't work assuming this script is in a GUI because the player would have already joined the game by the time the script loads. You're also comparing a userId(which is an int) with a string Thetacah 712 — 4y
0
JoshGamingHQ1, Everything in StarterGui gets replicated into PlayerGui(which is located inside the player) upon spawning. Thetacah 712 — 4y

Answer this question