How to make admin command that gives you cash?

Hi I have a question so I’m trying to make an admin command but it’s an admin command that gives you cash now I know how to make that but I want to make it so that you enter the command which will be “;cash” but after cash I want to make so you chose how much amount of money you get so if someone enters “;cash 1320” and they get 1320 cash please reply would appreciate and would really helpful :slight_smile: thank you

2 Likes

The easiest way to do this is pattern matching, which is a way of matching a specific pattern in a string. In this case, you want to match against a series of digits after “;cash” which is thankfully pretty easy:

local function GetMoneyFromCommand(command)
   local money = command:match(";cash (%d+)")
   return tonumber(money)
end

In the match method there, we capture “%d+” which means that we want all digits at that location returned in our variable. If it fails to match, it will return nil. This is important to check for.

We could then use it as such:

player.Chatted:Connect(function(message)
   local money = GetMoneyFromCommand(message)
   if money then
      -- Do something with `money` here
   end
end)

Use a PlayerAdded event and Chatted event, split the message, check if it was the correct command, and increase the player’s currency value.

local Players = game:GetService('Players') -- Get the "Players" service

local admins = { -- A table of people that can run the command
game.CreatorId
}

Players.PlayerAdded:Connect(function(player) -- Connect the PlayerAdded event
if not table.find(admins, player.UserId) then -- Check if the player can run the command.
return -- If not then we'll skip connecting the Chatted event
end

player.Chatted:Connect(function(message) -- Connect a Chatted event so we can detect if the player has chatted.
local split = string.split(message, ' ') -- Turn the message to a table

if split[1] == ';cash' then -- If our first index matches to ";cash"
player.leaderstats.Cash.Value += tonumber(split[2]) or 100 --[[ 
Increase the cash by the number of cash we are giving,
if it the 2nd index was nil then it will automatically be 100
]]
end
end)
end)
I have no idea why I put that many comments in the code.

Also I think that @sleitnick’s solution is better than mine.

edit: I think I read the title incorrectly lol.
edit 2: Alright, fixed it so that it gives it to yourself.

4 Likes

Is that the only code I need to use?

1 Like

Yo tysm it actually worked saved me a lot of time thx i really appreciate it