Issue With Chat Command

so, i’m trying to make a custom chat command, where if you say “x number” then “cats for you”, it will create that number of cat images. but for some reason, when i try to convert the prefix into a suffix, it only floods the output with warnings.

here’s what it looks like with the prefix.

my script when i’m using a prefix:

local playerService = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")
local catPart = replicatedStorage:FindFirstChild("catPart")
local parentFolder = workspace:FindFirstChild("catPartFolder")
local debris = game:GetService("Debris")
local prefixTrigger = "i cast:"

function playerAdded(player:Player)
	local character = player.Character or player.CharacterAdded:Wait()
	
	local function createImageBall(imageNum:number, targetPos:Vector3)
		local catBallClone = catPart:Clone()
		catBallClone.Parent = parentFolder
		catBallClone.Position = Vector3.new(targetPos.X, (targetPos.Y + 3), targetPos.Z)
		debris:AddItem(catBallClone, 7)
	end
	
	local function filterMessage(message:string, targetPos:Vector3)
		local _, _, castPrefix, catCount = string.find(message, "(" .. prefixTrigger .. " (%d+))")

		if castPrefix then
			-- player said "i cast:" followed by a number
			local numberOfCats = tonumber(catCount)

			if numberOfCats then
				for i = 1, math.clamp(numberOfCats, 0, 350) do
					createImageBall(numberOfCats, character.PrimaryPart.Position)
				end
			end
		end
	end
	
	player.Chatted:Connect(function(message)
		filterMessage(message)
	end)
end

playerService.PlayerAdded:Connect(playerAdded)

my script when i try to convert the prefix into a suffix:

local playerService = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")
local catPart = replicatedStorage:FindFirstChild("catPart")
local parentFolder = workspace:FindFirstChild("catPartFolder")
local debris = game:GetService("Debris")
local suffixTrigger = "cats for you" -- suffix trigger

function playerAdded(player)
	local character = player.Character or player.CharacterAdded:Wait()

	local function createImageBall(imageNum, targetPos)
		local catBallClone = catPart:Clone()
		catBallClone.Parent = parentFolder
		catBallClone.Position = Vector3.new(targetPos.X, targetPos.Y + 3, targetPos.Z)
		debris:AddItem(catBallClone, 7)
	end

	local function filterMessage(message, targetPos)
		print("received message:", message)

		local _, _, catCount, castSuffix = string.find(message, "(%d+) " .. suffixTrigger)

		if castSuffix then
			local numberOfCats = tonumber(catCount)

			if numberOfCats then
				for _ = 1, math.clamp(numberOfCats, 0, 350) do
					createImageBall(numberOfCats, character.PrimaryPart.Position)
				end
			end
		else
			warn("invalid message format: " .. message) -- it only produces warnings.
		end
	end

	player.Chatted:Connect(function(message)
		filterMessage(message)
	end)
end

playerService.PlayerAdded:Connect(playerAdded)

Hi,

Try using the string.split method instead:

	local function filterMessage(message, targetPos)
		print("received message:", message)

		local string_parts = string.split(message, " ")

        --[[
        For the new "if" statement below:

        First part makes sure that the first argument is a number
        Second part checks if the suffix is there
        Third part excludes the message if there is anything said after
        the whole chat command (you can remove this check if you want to)
        --]]
		if tonumber(string_parts[1]) and string_parts[2] == suffixTrigger and not string_parts[3] then
			local numberOfCats = tonumber(string_parts[1])

			if numberOfCats then
				for _ = 1, math.clamp(numberOfCats, 0, 350) do
					createImageBall(numberOfCats, character.PrimaryPart.Position)
				end
			end
		else
			warn("invalid message format: " .. message) -- it only produces warnings.
		end
	end
2 Likes

You forgot to capture the suffixTrigger in the postfix script. I don’t recommend this approach, though. Use string.match instead and only capture the catCount.

If the entire pattern isn’t matched, nothing is captured, meaning it is unnecessary for you to capture the prefix / suffix. It’s just boilerplate.

local catCount = string.match(message, "^(%d+) " .. suffixTrigger)

if catCount then
     -- pattern matching successful
end
1 Like