Help getting a message from an announcement command

Im trying to make a script that announces everything after !shout, here’s what I have so far:

game.Players.PlayerAdded:connect(function(plr)
	plr.Chatted:connect(function(msg)
	local args = msg:split(",")
	if args[1] ==  "!shout"
then
	if plr:GetRankInGroup(groupId)>= 198 then
		local message = args[2]
		print(args[1])
  print(args[2])
		local s = api.shout(groupId, message)
	end

	end

end)
end)

I’m not the best with using msg:split. Thank you for any help. :smile:

Just replace args variable with this fixed version to split your message correctly which I am going to post below.
You should use a whitespace remover instead of a comma, because probably you don’t want players to use the command as !shout, messagehere but you want to use it as !shout messagehere.

local args = string.split(msg," ")

The message sent next to command !shout shall be defined as args[2].

You can just use string.split() with whitespace to separate for the command, then check if it’s a valid command. If the command is valid, then we’ll use string.sub() and get the length of the command and add 2 places for spaces.

game.Players.PlayerAdded:Connect(function(plr)
	
	plr.Chatted:Connect(function(msg)
		local messageArgs = msg:split(" ")					
						
		if messageArgs[1]:lower() == "!shout" and plr:GetRankInGroup(groupId) >= 198 then
			
			local commandReason = msg:sub(messageArgs[1]:len() + 2,-1)
			local s = api.shout(groupId, commandReason)
			
			print(plr.Name .. " has shouted : " .. commandReason) -- Debug
			
		end
	
	end)
	
end)
1 Like