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

Creating a game chat function which detects a specific Player?

game.Players.PlayerAdded:connect (function(player)
	player.Chatted:connect(function (msg)
		if msg == "Material" then
			game.Workspace.BaseDoor.Material = Enum.Material.Plastic
			game.Workspace.WallA.Material = Enum.Material.Plastic
			game.Workspace.WallB.Material = Enum.Material.Plastic
			game.Workspace.Walls.Material = Enum.Material.Plastic
		end
	end)
end)

This script currently does the list below 'then' once a player types in chat "Material" but the problem is, anyone can call this command in chat. How can I make it so only specific players can call this command?

4 answers

✓ Accepted answer

There are some easy ways to do this

For Certain Commands Player Specific:

local Player_Accepted = "EasilyTech";

game.Players.PlayerAdded:connect (function(player)
    player.Chatted:connect(function (msg)
        if msg == "Material" and player.Name == Player_Accepted then
            game.Workspace.BaseDoor.Material = Enum.Material.Plastic
            game.Workspace.WallA.Material = Enum.Material.Plastic
            game.Workspace.WallB.Material = Enum.Material.Plastic
            game.Workspace.Walls.Material = Enum.Material.Plastic
	elseif msg == "reset" then
		player.Character:BreakJoints();
        end
    end)
end)

All Commands Player Specific:

local Player_Accepted = "EasilyTech";

game.Players.PlayerAdded:connect (function(player)
	if player.Name == Player_Accepted then
	    player.Chatted:connect(function (msg)
	        if msg == "Material" then
	            game.Workspace.BaseDoor.Material = Enum.Material.Plastic
	            game.Workspace.WallA.Material = Enum.Material.Plastic
	            game.Workspace.WallB.Material = Enum.Material.Plastic
	            game.Workspace.Walls.Material = Enum.Material.Plastic
	        end
	    end)
	end;
end)

These are just a few simple ways of doing this. ** Please note: This only works with the specified username. It won't work for them anymore if they change their name. **

I hope this helped. If it did please accept this as the answer so that others know that you have been helped and a solution has been found. If it didn't help feel free to message me and ask whatever you need to. If you have other questions again feel free to message me and ask for help.

Thank you to TheAlphaStigma for reminding me to add this:

local Players_Accepted = {["EasilyTech"[ = true, ["YourFriendHere"] = true};

game.Players.PlayerAdded:connect (function(player)
	if Players_Accepted[player.Name] then
	    player.Chatted:connect(function (msg)
	        if msg == "Material" then
	            game.Workspace.BaseDoor.Material = Enum.Material.Plastic
	            game.Workspace.WallA.Material = Enum.Material.Plastic
	            game.Workspace.WallB.Material = Enum.Material.Plastic
	            game.Workspace.Walls.Material = Enum.Material.Plastic
	        end
	    end)
	end;
end)

This one will check if the player is in a table and should be allowed to chat based on a list of players.

5 comments

0

Another way, if you want multiple players to be able to use the command, is to use the table w/ brackets. "local tbl = {['Player1'] = true, ['Player2'] = true}" (Haven't scripted in months, so go easy on me.)

0

I was thinking of adding that but I didn't let me add it.

0

You forgot the brackets around the names lol.

0

shhhh

0

I was tired. xD

You already have the player variable specified for you when the player joins the game and the script handles it in the first line. Simply add a clause to the if statement to check if the player name is the specific one you want.

player_name = "EasilyTech"

game.Players.PlayerAdded:connect (function(player)
    player.Chatted:connect(function (msg)
        if msg == "Material" and player == player_name then --Checks if it is you that chatted "Material" and then proceeds
            game.Workspace.BaseDoor.Material = Enum.Material.Plastic
            game.Workspace.WallA.Material = Enum.Material.Plastic
            game.Workspace.WallB.Material = Enum.Material.Plastic
            game.Workspace.Walls.Material = Enum.Material.Plastic
        end
    end)
end)

Expanding on MrLonely1221's post, you could also use a table to be able to accomplish this:

local PlayersWhoCanUseCommand = { -- The table which will be used later on
	['EasilyTech'] = true, -- This will be checked to see if the name is in the list later on
	['N00B'] = true
}

function onPlayerJoin(plr) -- I liked to keep my code kinda neat ;P onPlayerJoin is a function, and variable 'plr' is used to represent a player that has connected to the server
	if PlayersWhoCanUseCommand[plr.Name] then -- If the player's name is in the list, then it will return true & execute the following chunk, if not, well, you know
		plr.Chatted:connect(function(msg) -- Fires whenever a player chats; variable 'msg' is used to retrieve & return the player's chat
			if msg == 'command' then -- If the chat is 'command,' or the player chatted 'command,' it will return true & execute the following chunk
				print('Command fired') -- Prints into the Output
			end -- Ends the chunk
		end) -- Ends the function/event-chunk
	end -- Ends chunk
end -- Ends chunk

game.Players.PlayerAdded:connect(onPlayerJoin) -- Whenever a player connects to the server, the 'PlayerAdded' event will fire (Will only work in sever-scripts), and also fire & return the player, which is why 'plr' was added in the 'onPlayerJoin' function

Imo, I believe that's the best way to go about tables w/ chat commands: it's much simpler, and easier to use rather than having a for loop loop through a table & return a bool.

I haven't scripted in months, so I hope I helped you somehow. :)

local admins = {"EasilyTech", "yourbestfriend", "yourreallybestfriend"}
local banned = {"someoneyoudespise"}
game.Players.PlayerAdded:connect (function(player)
for _, s in pairs(banned) do
if player.Name == s then
player:Kick("#Banned") --Remove banned players
end
end
    player.Chatted:connect(function (msg)
for i, v in pairs(admins) do
        if msg == "Material" and player.Name == v then --If player's name is anyone at the table 
            game.Workspace.BaseDoor.Material = Enum.Material.Plastic
            game.Workspace.WallA.Material = Enum.Material.Plastic
            game.Workspace.WallB.Material = Enum.Material.Plastic
            game.Workspace.Walls.Material = Enum.Material.Plastic
        end
end
    end)
end)

1 comment

0

Do not just give code: it doesn't help the player to understand the reasoning behind your answer.

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