Detecting a chat command

Hello fellow developers! I’m a new coder introduced and was recently working on a project, I’m making a money system and want the player to drop cash on the command /dropcash (amount)
EX: /dropcash 50

How do I use chatted event to detect exactly /dropcash along with a number, I don’t really know that much about strings.
Thanks in advance

5 Likes

AlvinBlox has a cool tutorial on making Admin Commands, and goes over many “String Manipulating” techniques. Here is the tutorial, i hope this helps :slight_smile:

4 Likes

Alright thank you I’ll check it out

For illustrative purposes, since I don’t think you necessarily need a whole admin system, here’s a pretty basic way:

local function DropCash(player, amount)
    -- do whatever here
end

local function OnChatted(player, message)
    local starts, ends = message:find("/dropcash")

    -- check that we found "/dropcash" at the start of the string
    if (starts == 1) then
        -- just gets the next word ("%W" means "any non-whitespace", and "+" 
        -- means "one or more")
        local followingWord = message:match("%W+", ends)

        -- convert to a number, call DropCash
        local num = tonumber(followingWord)
        DropCash(player, num)
    end
end

local function OnPlayerAdded(player)
    player.Chatted:Connect(function(msg) OnChatted(player, msg) end)
end

-- connect for any players that have already entered the game

for _, player in pairs(game.Players:GetPlayers()) do
    OnPlayerAdded(player)
end

-- connect for any new players

game.Players.PlayerAdded:Connect(OnPlayerAdded)
1 Like

I recommended the video because it goes over player.Chatted events and string manipulation

I tried your method but on this specific line:

local starts, ends = message:find("/dropcash")

It errors “attempt to index nil with find”

Ah yeah my bad, fixed I think.

It works! here is the issue, while printing the player it works, but when printing the amount it prints “nil”.
I’ll just have a look at the video P1X3L sent, thanks for the code anyways!

Maybe this could help you:

local players = game:GetService("Players")	--Gets the player service, more secure way to game.Players
local command = "/dropcash"	--The command as a string

local function dropCash(Amount_of_money) --The function, it's empty though
	...
end

players.PlayerAdded:Connect(function(plr) --This fires when a player joins
	plr.Chatted:Connect(function(msg) --This fires when the player that joined chats
		local splits = msg:split(" ") -- "Hello there!" would split into [1] = "Hello", [2] = "there!" now
		if splits[1]:lower() == command then --Checks if he said the command
			--He said the command
			--Now splits[2] should be the amount of money, right?
			local AMOUNT_OF_MONEY = tonumber(splits[2]) --Turns the string number into a number
			dropCash(AMOUNT_OF_MONEY) --Fires the function which is empty
		end
	end)
end)
3 Likes

Hmm, there is an issue, here is my code:

local command = "/dropcash"

local function DROP_CASH(player, amount)
    print(player.." dropped "..amount"$")
end

game.Players.PlayerAdded:Connect(function(plr)
    plr.Chatted:Connect(function(message)
       local splitstring = message:split(" ")
       if splitstring[1]:lower() == command then
          local MONEY_AMOUNT = tonumber(splitstring[2])
          DROP_CASH(plr)
       end
   end)
end

if I do the command /dropcash 50 it says “attempt to call a nil value”, apparently it’s returning the player but not the amount of cash dropped.

(nvm just realized I had function ran with 1 argument only)

1 Like

Yep. You should do

local command = "/dropcash"

local function DROP_CASH(player, amount)
    print(player.." dropped "..amount"$")
end

game.Players.PlayerAdded:Connect(function(plr)
    plr.Chatted:Connect(function(message)
       local splitstring = message:split(" ")
       if splitstring[1]:lower() == command then
          local MONEY_AMOUNT = tonumber(splitstring[2])
          DROP_CASH(plr, MONEY_AMOUNT)
       end
   end)
end
1 Like