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

How would I go about doing a Kick Player chat command?

Hello! Basically I want to have " :kick (player name) (reason) ", however, I cannot figure out how I could get the Player Name and the Reason as separate strings.

I've got something like:

game.Players.PlayerAdded:connect(function(p)
	p.Chatted:connect(function(c)
		if string.sub(c,1,5) == ":kick" then
			-- Need help on this bit, cheers!
		end
	end)
end)

Cheers,

3 answers

✓ Accepted answer
game.Players.PlayerAdded:connect(function(p)
    p.Chatted:connect(function(c)
        if string.sub(c,1,5) == ":kick" then
          local raw = string.split(c, " ")
          local targetPlayerName = raw[2]
          local reason = raw[3] == nil and "Unspecified reason" or raw[3]
          if game.Players:FindFirstChild(targetPlayerName) then
            game.Players:FindFirstChild(targetPlayerName):Kick(reason)
        end
        end
    end)
end)

You'll find string.split() very useful in this case, which returns a table with the strings separated by the character you choose. More on this here.

I put it together for you, didn't do the kicking part for you though, I'm guessing you're capable of doing that.

game.Players.PlayerAdded:connect(function(p)
    p.Chatted:connect(function(c)
        if string.sub(c,1,5) == ":kick" then
			local Separator = string.split(string.sub(c, 6), " ")
			local playerName = Separator[2]:lower() -- gets the player name
			local msg = Separator[3] -- gets the last bit of text
			local plrObj 
			for i, v in pairs(game.Players:GetPlayers()) do -- match playername w player
				if string.match(string.lower(v.Name), playerName) then
					plrObj = v
					plrObj:Kick(msg)
				end
			end
            -- Need help on this bit, cheers!
        end
    end)
end)

Hope I helped. If you have any questions please let me know.

Something I whipped up real fast. Would have to do some more tinkering if wanted a "spaced out" message.

game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:connect(function(msg)
		local newMsg = msg:split(" ")
		if newMsg[1] == ":kick" then
			game.Players[newMsg[2]]:Kick(newMsg[3])
		end
	end)
end)

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