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

How do I make a command with a player?

Alright, I've tried to ask this question 2 times before, but here's what happened: I was halfway done, and I was going to press preview, but I accidentally clicked ask question and had to delete it. I know I could've just edited it, but it would be confusing if you saw a halfway done question on the front page. Also, it takes me a bit to finish the other half. I'm tired of writing this over and over and just want to finish this question and get some answers. Anyways, let's start.

I'm trying to make a admin here. I'm have finished some commands, but I'm having trouble with those commands where you need a player name like Loot_0, 0_tooL, and some others. An example of a command with a player on it is a :kill command which is the command I want. Here's what I want to do: I want to make a command that needs a player. Here is my code:

--Note that I wrote this code on here so there may be mistakes.
local admins = {Loot_0, 0_tooL}
local prefix = "-"

game.Players.PlayerAdded:Connect(function(plr)
     plr.Chatted:Connect(function(msg)
         if msg == prefix.."kill "..PlayerName then
              workspace.PlayerName.Health = 0
         end
     end
end)

Now, at line 7 and 8, I know they have an error, and I want that to be fixed. another thing, I want it so you don't have to type out the full name and not have to put the capitals. One more thing, I want to figure out how to make it so if you say me, others, all, It will kill all, others, or just you.

2 comments

0

There are lots of threads on this. Google "Admin commands ROBLOX devforum"

0

I have seen them, but they are really confusing.

1 answer

✓ Accepted answer

The trick here is to split up the text by spaces and separate the command from the arguments, almost like a lexer would if you were making a compiler or interpreter. We can do this by using the split method in the string library.

local admins = {"Loot_0", "0_tooL"}
local prefix = "-"

game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg, r) -- r is the recipient
		if r then return end -- if there is a recipient it means they are sending a private message so we'll return
		if string.sub(msg, 1, 1) ~= prefix then return end -- if it does not start with the prefix return

		msg = string.sub(msg, 2, #msg) -- remove the prefix from the message
		local args = string.split(msg, " ") -- splitting it by a space to get the different words 
		local cmd = table.remove(args, 1)  -- removing the first word (the command) from the arguments, table.remove returns the removed value

		if cmd == "kill" then
			if not game.Players[args[1]] return end -- this command only uses one extra word, the player name, so it will be the first argument
			-- execute command
		end
		
	end
end)

2 comments

0

I'm late but, i will test this and will report back if it works.

0

alright so im back, yes, its been 8 months. but you see, i did try this 8 months ago, and i got errors. i also forgot about this. but i probably just did pasted it wrong. my scripting has improved, and i can see that it would work, so ill accept the answer.

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