Help with string.split using chat messages

Okay so, I am attempting to script something in ServerScriptService that registers when a player chats “;jail” and then gets the name of the user after it, like “;jail MochaTheDev.” I honestly have no clue how to use string.split so could anyone tell me? ALSO: The user needs to be in a specific roblox group for it to work. This is what I have so far;

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg) 
		if msg == "example" and player:IsInGroup(12345678) then
			
		end
	end)
end)

I have a youtube video on how to do something like this. It is unlisted along with all my videos but you can still get to it through the link here.

Heres some lazy old code I wrote in around 5 minutes.

game.Players.PlayerAdded:Connect(function(player)
  player.Chatted:Connect(function(msg)
    local splitted = string.split(msg," ")
    if splitted[1] == ";jail" and player:IsInGroup(YOURGROUPID) then
      local plr = game.Players:FindFirstChild(splitted[2])
      if plr then
        --// just do whatever you want to do with the player here
      end
    end
  end)
end)

This should work. I dont know.
You can also use string.sub() to detect player’s names with spaces aswell.

This is hilarious, wrote some exact code for my friend the other day… Here, hope this helps.

The command in chat would be something like; “#jail | crem”

local groupId = 13609244
local rank = 255

local JailTeleportPart = jailPart
local spawnPart = part --part you want the player to return to after being released

function getplr(givenstring)
    for _,possplr in pairs(game.Players:GetPlayers()) do
        local possplrname = possplr.Name
        if possplrname:lower():find(givenstring) then
            return possplr
        end
    end
end

game.Players.PlayerAdded:Connect(function(Player)
    if Player:GetRankInGroup(groupId) >= rank then
        Player.Chatted:Connect(function(Message)
            local Words = string.split(Message, " | ")

            if Words[1] == "#jail" then
                local PTJ = getplr(Words[2])
                if PTJ ~= nil then
                    for _,gui in pairs(PTJ.PlayerGui:GetChildren()) do
                      if gui:IsA("ScreenGui") then
                        gui.Enabled = false
                      end
                    end
                    PTJ.Character:SetPrimaryPartCFrame(JailTeleportPart.CFrame)
                    wait(5)
                   for _,gui in pairs(PTJ.PlayerGui:GetChildren()) do
                      if gui:IsA("ScreenGui") then
                        gui.Enabled = true
                      end
                    end
                    PTJ.Character:SetPrimaryPartCFrame(spawnPart.CFrame)
                else
                    -- error message goes here, smth like "player could not be found etc
                end
            end
        end)
    end
end)

I was also struggling with this awhile back. Here is a forum post that should help you out.

Gotchu. I will explain everything for your case:

Starter code is what you have, minus this:

Ok, so what you want to do is first split the string by spaces:
local splitMsg = msg:split(" ")

Then what you want to do is get (maybe) the command and the playerName. Note I said maybe because it could be a normal message that’s why we need a few if statements to check:

local command = splitMsg[1]
local playerName = splitMsg[2]

if command == ";jail" then
    if game.Players:FindFirstChild(playerName) then
        local plr = game.Players:FindFirstChild(playerName)
    end
end

Now, execute your code. It’s that simple!
Example code for a kick command:


game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg) 
        local splitMsg = msg:split(" ")
        local command = splitMsg[1]
        local playerName = splitMsg[2]

        if command == ";kick" then
            if game.Players:FindFirstChild(playerName) then
                local plr = game.Players:FindFirstChild(playerName)
                plr:Kick("You’ve been kicked by someone")
           end
        end
    end)
end)

Hope that clears things up.

Edits: formatting