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

Admin command help???

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

✓ Accepted answer

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)

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)

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.

1 comment

0

There's also another error with the script, he's running a generic for loop with 'Performer', an Instance.

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