Need help with kick command

Sorry for being annoying I need a bit of help with my kick command, every time I type “kick all” it adds another reason like first player sees “kicked for hello” second player sees “kicked for hello hello” exp;

local prefix = "/"
local players = game:GetService("Players")


function GetPlayerFromServer(Player,String)
	local Found = {}
	local strl = String:lower()
	if strl == "all" then
		for i,v in pairs(game:GetService("Players"):GetPlayers()) do
			table.insert(Found,v)
		end
	elseif strl == "others" then
		for i,v in pairs(game:GetService("Players"):GetPlayers()) do
			if v.Name ~= Player.Name then
				table.insert(Found,v)
			end
		end   
	elseif strl == "me" then
		for i,v in pairs(game:GetService("Players"):GetPlayers()) do
			if v.Name == Player.Name then
				table.insert(Found,v)
			end
		end  
	else
		for i,v in pairs(game:GetService("Players"):GetPlayers()) do
			if v.Name:lower():sub(1, #String) == String:lower() then
				table.insert(Found,v)
			end
		end    
	end
	return Found    
end

players.PlayerAdded:Connect(function(plr) -- getting the player that joined the server
			plr.Chatted:Connect(function(msg)
				local split = string.split(msg," ")
				-- cutting the table into segmants by a letter " "

				-- checking to see if they said the command

		if string.lower(split[1]) == prefix.."kick" then -- /kick
					local reason = ""
					-- remember this? We are going to change this now.

					-- the 1st parameter is the local player (You) and the 2nd parameter is a string
		   	for _,Player in ipairs(GetPlayerFromServer(plr,split[2])) do

						for i,v in pairs(split) do -- Getting all the seperated parts of the table
							if i == 1 or i == 2 then -- 1 is the [command we said] /kick and 2 is the [player's name we said] victim
							else
								reason=reason.." "..split[i] -- We are combining the words back together after cutting them but removing 2 words
							end

						end

				  		if reason == "" then
				     	Player:Kick("You have been kicked by "..plr.Name.." for: no reason")
						else
					    Player:Kick("You have been kicked by "..plr.Name.." for: "..reason)
				end

			end
		end

	end)
end)

the whole code.

1 Like

stop using split!!! match makes it gazillion times easier.

local arg1,arg2,reason = string.match(str,"(%w+)%s+(%w+)%s+(.+)")
for i,v in pairs(split) do -- Getting all the seperated parts of the table
	if i == 1 or i == 2 then -- 1 is the [command we said] /kick and 2 is the [player's name we said] victim
	else
		reason=reason.." "..split[i] -- We are combining the words back together after cutting them but removing 2 words
	end

end

This loop is ran for each player iterated over, declare ‘reason’ locally inside the loop, that way it is reset for each cycle (for each player).

This is imperformant because the string you want to concentate will stay in memory, your better off putting that in table and concatting the table when your done.

No it won’t, variables declared within the body of a loop construct are garbage collected at the end of each iteration.

for _ = 1, 10 do
	local Variable = "Hello world!" --Garbage collected at the next garbage collection cycle after the iteration.
end