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

Admin command help???

Asked by 8 years ago

I can't get it to find the Player's Name here was my method I attempted this

local Admins = {"Player"}

game.Players.PlayerAdded:connect(function(Performer)
    for i,v in pairs (Performer) do
        if v.Name == Admins then
            Performer.Chatted:connect(function(Command)
                if string.sub(Command, 1, 4) == "msg/" then
                    local MSG = Instance.new("Message", game.Workspace)

                    MSG.Text = string.sub(Command, 5)
                end
            end)
        end
    end
end)

3 answers

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
8 years ago

Your Problem

Firstly, the first, and only, value returned from the PlayerAdded event is not a table, it is a Player instance of the new player that joined. It cannot be used as an argument for the Pairs iterator function.


Code

local Admins = {"Player","Goulstem"}

game.Players.PlayerAdded:connect(function(Performer)
    for i,v in pairs (Admins) do
        if v == Performer.Name then
            Performer.Chatted:connect(function(Command)
                if string.sub(Command, 1, 4) == "msg/" then
                    local MSG = Instance.new("Message", workspace)
                    MSG.Text = string.sub(Command, 5)
                end
            end)
        end
    end
end)
Ad
Log in to vote
2
Answered by 8 years ago

It's comparing a string (v.Name) to a table, it'll always be false because a string doesn't equal a table no matter what the values.

Also, you have a for loop running when "Performer" is an instance not a table. No idea why you're doing that so I just removed it.

You should write a function to check if a player is an admin (forgive my formatting, I'm writing this in the editor :P)

local function isAdmin(player)
for i, v in pairs(Admins) do --for all the admins in the admin list
if v == player.Name then --if the player's name matches an admin's name
return true --end the function and return true
end
end
return false  --if the function is still running at the end of the loop the player is not an admin.
end

Updated code:

local Admins = {"Player"}

local function isAdmin(player)
for i, v in pairs(Admins) do --for all the admins in the admin list
if v == player.Name then --if the player's name matches an admin's name
return true --end the function and return true
end
end
return false  --if the function is still running at the end of the loop the player is not an admin.
end

game.Players.PlayerAdded:connect(function(Performer)
        if isAdmin(Performer) then
            Performer.Chatted:connect(function(Command)
                if string.sub(Command, 1, 4) == "msg/" then
                    local MSG = Instance.new("Message", game.Workspace)

                    MSG.Text = string.sub(Command, 5)
                end
            end)
        end
end)
Log in to vote
1
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
8 years ago

if v.Name == Admins is not true because a string is not equal to a table (unless the __eq metamethod accounts for that.)

You want to find if the table contains the string, and not if the string and the table are the same thing. This is one way to do that:

local Admins = {
    ["Player"]=true
}

...

if Admins[Performer.Name] then

You're also trying to iterate through the Player object like it's a table for some reason. You don't need to do that.

0
There's also another error with the script, he's running a generic for loop with 'Performer', an Instance. YellowoTide 1992 — 8y

Answer this question