Message:split() Not working correctly

I’m working on an Admin Script but it seems that split() isn’t getting the rest of the text placed after, if someone could help me, that would be great.

ServerScript To Fetch Announcement Chat
local function msgAnnounce(plr, msg)
	local split = msg:split(" ")
	local cmd = split[1]
	local txt = split[2]
	
	local newUI = game.StarterGui.AUI
	newUI.back.CD.Text = plr
	newUI.back.Desc.Text = txt
	
	game.ReplicatedStorage.AN:FireAllClients(plr, txt)
	
	wait(8)
	
	newUI.back.CD.Text = ""
	newUI.back.Desc.Text = ""
	
	print("Cycle Complete")
	
	end


game.Players.PlayerAdded:Connect(function(plr)
	if plr:GetRankInGroup(3091517) >= 65 then
		plr.Chatted:Connect(function(msg)
			local msg1 = msg:split(" ")
			if msg1[1] == "+A" or "+a" then
				msgAnnounce(plr.Name, msg)
			else
				print("no")
			end
		end)
	end
end)
LocalScript To Change Stuffs cuz yes
game.ReplicatedStorage.AN.OnClientEvent:Connect(function(playr, txt)
	local plr = game.Players.LocalPlayer
	
	script.Parent.back.Desc.Text = txt
	script.Parent.back.CD.Text = playr
	
	script.Parent.Enabled = true
	
	wait(8)
	
	script.Parent.Enabled = false
	script.Parent.back.Desc.Text = ""
	script.Parent.back.CD.Text = ""
end)

What exactly do you mean? Is the txt variable not being set correctly? Apologies, just trying to get an understanding.

If you have a space after split[2] you’ll need to concatenate it with the split[2] value.

Ah, it’s my bad if I didn’t explain it fully.

So, after the +A there is the text right after it which would/should be corresponding to split[2]
but, it seems that it only gets 1 word from that area and not the rest of it, leaving it unused.

Try this

local txt = ''
for i,v in pairs(split) do
if i >= 2 then
txt = txt..string.format('%s ', v)
end
end
1 Like

That is because it splits the string at every space, not just the first one. In order to do what you are trying to, revise your script to the following

local split = msg:split(" ")
local cmd = table.remove(split,1)
local txt = table.concat(split, " ")

EDIT: Made a slight update due to suggestion from @ItzEthanPlayz_YT (thank you)

1 Like

table.remove returns the content being removed, so you can simply just do:

local split = msg:split(" ")
local cmd = table.remove(split, 1)
local txt = table.concat(split, " ")

Oh nice, I didn’t know about that. I’ll edit my solution :slight_smile:

local newMsg = msg:gsub("^.*%s", "")

May as well turn 3 operations into 1.

1 Like