Help With Chat Feature

Hello! I’m trying to make it so when a mod chats “!erase”, all parts in a folder get destroyed. Help?

Code:

local mods = {198955588}

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(Message,Recipient)
		if Message == "!erase" then
			if table.find(mods, player.UserId) then
				for i,v in pairs(game.Workspace.Lines:GetChildren()) do
					v:Destroy()
				end
			end
		end
	end)
end)
1 Like

Does it not work? If it doesn’t work as expected, what does it do?

Oops - I was being stupid and mistyped something. :man_facepalming: Sorry about wasting your time.

Don’t worry about it, happens to everyone. If it’s not too much trouble, could you post your fix? Someone will almost certainly have a similar problem in the future and find this post by searching.

Sorry - another question:

How could I make it so you do like “!erase (random word)”, and it detects parts with said word. This is for a drawing game so I can erase a specific player’s drawing.

1 Like

There’s already some information out there, so do a search and see what you find!

But the basics of it is to split each chat message into words, and then do something based on which words were said. Here’s a really basic example:


local commands = {
	erase = function(playerName)
		local player = game.Players:FindFirstChild(playerName)
		if player then
			print("Erasing for " .. playerName)
			-- do whatever you need to do to the player
		end
	end,
	hurt = function(playerName, amounStr)
		local player = game.Players:FindFirstChild(playerName)
		local amount = tonumber(amounStr)
		if player and amount then
			local character = player.Character
			if character then
				local humanoid = character:FindFirstChildWhichIsA("Humanoid")
				if humanoid then
					humanoid.Health -= amount
				end
			end
		end
	end,
}

function executeCommandMessage(commandMessage)
	if not ( string.sub(commandMessage, 1, 1) == '!' ) then return end

	local wordsIter = string.gmatch(commandMessage, "%w+")
	local command
	local arguments = {}

	for word in wordsIter do
		if not command then 
			command = word 
		else
			table.insert(arguments , word)
		end
	end

	if commands[command] then
		commands[command](unpack(arguments))
	end
end

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		executeCommandMessage(message)
	end)
end)