How to restrict a script only for certain users?

I’m trying to find out how to make my command private only for some users, although I’m not finding any articles or places that answer my question.
I tried searching up some tutorials, but didn’t find any in the DevForum or the DevHub. I tried searching other places, but couldn’t find any.
Here’s my code:

p=game.Workspace.Curtains

function onChatted(msg, speaker)
	local source = string.lower(speaker.Name)
	msg = string.lower(msg)
	if msg == "/e !?opencurt" then
		for i=1,100 do
	p.CFrame=p.CFrame+Vector3.new(0,0,-1)
	wait()
end
	end
end

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:connect(function(msg)onChatted(msg, player) end)
end)

Any help is appreciated, thanks!

5 Likes

The simple way

On chatted speaker is a player. So you can check if they are an admin like this

if speaker.UserId == 1 or speaker.Name == "sloss2003" then

and then do something.

========================================

If you want to have an array of admin usernames you can do this

local admins = {"sloss2003"};
function checkAdmin(speaker)
    local isAdmin = false;
    for a,b in pairs (admins) do
        if b == speaker.Name then
            isAdmin = true;
            break;
        end
    end
    return isAdmin
end

then check if they are an admin like this

if isAdmin(speaker) then
2 Likes

Where should I add the simple way? Would it go after if msg == "/e !?opencurt" then
Sorry, a bit new to scripting. :sweat_smile:

I would add it before your command checks like this

p=game.Workspace.Curtains

function onChatted(msg, speaker)
    local source = string.lower(speaker.Name)
    if source == "sloss2003" --[[your name here]]then
        msg = string.lower(msg)
        if msg == "/e !?opencurt" then
            for i=1,100 do
                p.CFrame=p.CFrame+Vector3.new(0,0,-1)
                wait()
            end
        end
    end
end

game.Players.PlayerAdded:Connect(function(player)
    player.Chatted:connect(function(msg)onChatted(msg, player) end)
end)

Why?

This way you can write more commands under it with ease, without having to copy and paste isAdmin(speaker) into each if-statement.

Ah, that works, thanks! Last thing, if I wanted to allow multiple users, would I have to use the arrays?

You can, it’ll make your life easier. You can do it like this:

local admins = {"sloss2003"}; -- put your admin names here
local p=game.Workspace.Curtains

function checkAdmin(speaker)
    local isAdmin = false;
    for a,b in pairs (admins) do
        if b == speaker.Name then
            isAdmin = true;
            break;
        end
    end
    return isAdmin
end

function onChatted(msg, speaker)
    if checkAdmin(speaker) then
        local source = string.lower(speaker.Name)
        msg = string.lower(msg)
        if msg == "/e !?opencurt" then
            for i=1,100 do
                p.CFrame=p.CFrame+Vector3.new(0,0,-1)
                wait()
            end
        end
    end
end

game.Players.PlayerAdded:Connect(function(player)
    player.Chatted:connect(function(msg)onChatted(msg, player) end)
end)
1 Like

Alright, thanks so much! I appreciate your fast reply.

You’re welcome :slight_smile:
30chars

1 Like