Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How i can make a allowed player to open a gui?

Hi i have this code for an admin gui wich you can open with ;admin message on chat but how i can make it to be only selected user? local allowed not working...

My script

script.Parent.main.Active = true
script.Parent.main.Draggable = true

local plr = game.Players.LocalPlayer
local frame = script.Parent.main
local RS = game.ReplicatedStorage
local user = frame.user
local reason = frame.reason

plr.Chatted:Connect(function(msg)
	local allowed = ("Zypsoh" and "speedy_pt06")
	if msg:lower() == ";admin" then
		frame.Visible = true
	end
end)

frame.Kill.MouseButton1Click:Connect(function()
	RS.AdminGUI.KillPlayer:FireServer(user.Text)
end)

frame.Kick.MouseButton1Click:Connect(function()
	RS.AdminGUI.KickPlayer:FireServer(user.Text, reason.Text)
end)

MY DISCORD:Zypsoh#8546

1 answer

✓ Accepted answer

It's not working because you haven't told the code to filter the user. On line 11 the syntax is also illegal so you'll come across and compiling error.

You're actually looking for something called an array, or otherwise known as a table. This is a compact list of data that can be stored within a variable. It looks like so:

local array = {"One", "Two", "Three"}; --// Semicolon is optional.

These pieces of information are stored by index in ascending numerical order, which means the first piece of data is located at 1 and the second 2 and so on.

So for us to get the data within, we need to call it respectively, like so:

print(array[1]) -> "One"
print(array[2]) -> "Two"

We can also sort the index format to become a dictionary, not much changes here, except that the reference we pass can become specific, so instead of looking for their numerical placement, we find it by asking where it's stored under the given name. See below for an example.

local dictionary = {
	["Number1"] = "One",
	["Number2"] = "Two"
};

Let's now take a look at it once again.

print(dictionary["Number1"]) -> "One"

So now that we understand how to store our allowed players, we can properly set that up, but before doing so, it's actually a better practice to use the players UserIds, as these are unique static identifiers. Why is this important? Well, usernames are actually dynamic, as they can change, however, they do not within the code so once done the program will no longer recognize the user.

[Sidenote]: I looked up the users you had shown, and I implemented their IDs so there is no work needed on your end to set that up.

For the purpose of this program, we need to use a dictionary approach as we have to look for them under their UserIds. So it'll look like this:

local allowed = {
	[203078919] = "speedy_pt06", [1352379910] = "Zypsoh"
}

For this, we are simply just seeing if the ID exists, so whatever we set the value to what it's referencing doesn't matter as we aren't actually asking for what it holds. This is only necessary to initialize the UserId into the dictionary.

Now that we have our array set up, we need to condition the chatting listener to activate only for these specified users, we can do this by calling the listener within a conditional (if statement) when it verifies it's a match

The way we can achieve this is through a for loop. But not just any, a very special one that allows us to traverse tables. This is formally recognized as i,v pairs. it looks like this in short explanation:

for i,v in pairs(table) do
 --// Le'ts talk about what this is doing.
end

i and v are variables that take in information from pairs, and for pairs, you don't need to know what that does, it just basically breaks up the table so it can be iterated.

The first variable stands for Index, it just holds the current numerical placeholder the loop is at within the table. The second stands for Value, what the current piece of data is that is held at that index.

But we don't need Index for this, so we can actually pass it off by using and underscore to ignore it.

Now let's start setting up this loop. To actively pair the UserIds to the ones listed within the array, we need to go through every player within the game and match them. So, we need to get that list of players (which is formally an array too; which is why we can apply it here) and pass it into the pairs function.

local Players = game:GetService("Players") --// We need the Players container first.

for _,v in pairs(Players:GetPlayers()) do --// GetPlayers() returns the entire player list
	--// Here we then add the conditional match, and we're finished.
end

[One final note]: You can optionally name i or v to whatever fits your purpose, so for this example we'll change v to Player, to represent the current player we're on, as explained before.

Here is our final script!

local Players = game:GetService("Players") --// We need the Players container first.

local allowed = {
	[203078919] = "speedy_pt06", [1352379910] = "Zypsoh"
};

for _,Player in pairs(Players:GetPlayers()) do
	if (allowed[Player.UserId]) then --// Try to index the UserId
		Player.Chatted:Connect(function(msg)
			if (msg:lower() == ";admin") then
				Frame.Visible = true
			end
		end)
	end
end

Hope this works for you! if so, don't forget to accept, and upvote if you please!

If you have any additional questions, please ask!

Happy New Year!

2 comments

0

Happy new Year by friend! You are a very great person !

0

Zypsoh#8546 if you can add me on discord pls

This discussion is preserved as it was. New answers, comments, and votes are closed.