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

How to search for a space in chat?

This is for an admin script. How do I have it search for the second space? So like ``;;setHealth [Player] [newHealth]" I cant figure out how to have the script 'know' what part is the name and what part is the value that the health will be set to

1 answer

✓ Accepted answer

String patterns are just what you need. I recommend splitting the string after the signal character(s) (in your case, the ";;") into a command and argument.

local SignalCharacters = ";;"
local Message = ";;setHealth Username 100"

function HandleMessage(Message)
	if Message:sub(1, #SignalCharacters) ~= SignalCharacters then
		-- Return early if message isn't a command
		return
	end

	local CommandSubstring = Message:sub(#SignalCharacters + 1)

	local Command = nil
	local Arguments = {}

	for Match in CommandSubstring:gmatch("%w+") do
		-- If there's not a first match yet, it's the command!
		if  Command then
			table.insert(Arguments, Match)
		else
			-- If there's not a first match yet, it's the command!
			Command = Match
		end
	end

	if not Command then
		return
	end

	-- Continue here to check if it's a valid command, and has the valid number of arguments, et cetera.
end

HandleMessage(Message)

2 comments

0

Im afraid I dont quite understand what everything here does

0

I recommend you check out the Wiki article I linked so you'd understand a little more! %w is the word character class, which will match an alphanumeric character, and the + quantifier matches 1 or more of the previous specifier.

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