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

I need help to be able to input many usernames instead of just 1[mine], help?

Asked by 3 years ago

So basically i'm making a minigame game and i'm making a textbutton only visible to the usernames I put so they can tp to the host box and have easy access to every minigame without needing to fly miles away.

Here is the current script:

local Gui = game.ReplicatedStorage.HostOnlyGui
local Hosts = game.Players:WaitForChild("Nitrolux200")

wait()
Gui.Parent = Hosts.PlayerGui

I tried adding more usernames in "local Hosts" section but it didn't seem to work with multiple, it only works with 1. Can anyone help me know how to make it so I can place in many usernames instead of just 1 [mine] ?

2 answers

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

You're looking to take advantage of Tables, here specifically, the Dictionary variant. You can use this to store the UserIds (a static and unique identifier) of the Players you wish to authorize the usage of this UI within it, opening an efficient way of properly handling the distribution of your GUI.


Note: ReplicatedStorage is a shared space between the Client and Server. Giving an Instance with the magnitude of "host permissions" this home wouldn't be wise. Store it under the Script disbursing it—which should be located under ServerScriptService.


--###----------[[SERVICES]]----------###--
local Players = game:GetService("Players")



--###----------[[VARIABLES]]----------###--
local AuthorizedUsers = {

    [--[[USERID (MANDATORY)]]] = --// Username

}


local GUI = script.HostOnlyGui



--###----------[[INITIALIZATION]]----------###--
Players.PlayerAdded:Connect(function(Player)
    if (AuthorizedUsers[Player.UserId]) then
        ---------------
        GUI:Clone().Parent = Player.PlayerGui
    end
end)
0
Ziffix, in the "local AuthorizedUsers = {" how am I supposed to put the allowed users? Do I put their userid inside "" or no because I tried with the username, userid and all of the ways i thought about but it didn't work. Nitrolux200 62 — 3y
0
local AuthorityUsers = { [123456789] = "Username"; } Ziffixture 6913 — 3y
0
I use the UserId as key for efficiency and ease. To instantiate the key—which doesn't require anything specific—I use the respective Username to help understand who the UserId belongs to. Ziffixture 6913 — 3y
0
Not only that, the UserId is a static identifier; unlike the Name, it won't change, therefore you won't ever have to update the Script. Ziffixture 6913 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

You need to use tables.

local Gui = game.ReplicatedStorage.HostOnlyGui
local Hosts = {"Nitrolux200","SomeoneElse"}

wait()
for i,v in pairs(Hosts) do
    local Player = game.Players:WaitForChild(v)
    local newGui = Gui:Clone()
    newGui.Parent = Player.PlayerGui
end

Hope this helps!

0
Thank you! This works perfectly fine, although i'm going to wait for the other persons response to compare which one would suit me better. Nitrolux200 62 — 3y

Answer this question