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

I am trying to create admin commands for a game I am trying to make and IDK what to do?

admins = {"HydroDivinity"}

game.Players.PlayerAdded:connect(function(pl)
	for i,v in pairs(admins) do
		if v == pl.Name then
			pl.Chatted:connect(function(mes)
				if mes:sub(1,4) == "add " then
					print(pl.Name.." is running the "..mes:sub(1,3).." command")
					if mes:sub(5,7) == "c " then
						print(pl.Name.." is changing somebody's coin-value")
					end
				end
			end)
		end
	end
end)

Alright, so basically, I have a gui in-game for currency. I want admins to be able to change people's currency depending on what they write on the cmd.

For example;

add c HydroDivinity 6

Not sure how to make "HydroDivinity" come before the 6 value

1 comment

0

This question is very well put together. You highlight the needs with an example, give a small code sample (not posting the entire thing), and clearly state your question at the bottom. Try to make your title more vague next time however. This is too specific with "admin commands". Instead talk about finding patterns in strings.

1 answer

✓ Accepted answer

There are a few ways to do this, but I think the best way would be to use string patterns.

I will use the pattern (%w+) (%d+)%s*$. This translates to:\

  • look for the word add and a space

  • look for multiple alphanumeric characters (A-z, 0-9) at least one and save it (parenthesis make it a capture group)

  • look for multiple digits at least one and save it

  • look for any whitespace, okay if I don't find any

  • end of string $

Let's try this out (you can do so online with the lua demo site here) with some code.

 str = "add kools 10"
 name, amount = str:match("(%w+) (%d+)$")
 print(name)
 print(amount)

...

kools

10

Cool, let's apply this to your code.

pl.Chatted:connect(function(mes)
   if mes:sub(1,4) == "add" then
      name, value = mes:match("(%w+) (%d+)$") -- If there is no match, this will be nil.
      if name and value then
          print(pl.Name.." is changing "..name.."'s coin value by ".. value)
      end
   end
end)

You can of course adapt these patterns to work better for yourself, and use them when determining change as well because it has the same form.

1 comment

0

dude, use local variables they are quicker to access

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