How do you get the rest of a string cause I can only get the first word of it?

So recently I have been creating a Admin Panel GUI and I am currently working on a kick function. Sadly, whenever I kick the player, I only get the first word of the third argument, and not the rest, I am wondering how I get the rest of the argument? Any help is appreciated and thanks in advance! :slight_smile:

Here is my code:

The kick function is at the bottom

local manualbanned = {"NACKRR"}
local admins = {"NACKRR"}
local prefix = ";"
local dss = game:GetService("DataStoreService")
local bds = dss:GetDataStore("BanDataStore")

function checkadmin(name)
	for i,v in pairs(admins) do
		if v == name then return true
		else
			return false	
		end
	end
end

function setoutput(plr,text)
	plr.PlayerGui.AdminPanel.CommandBackground.Output.Text = tostring(text)
	wait(5)
	plr.PlayerGui.AdminPanel.CommandBackground.Output.Text = ""
end

game.Players.PlayerAdded:Connect(function(player)
	local userid = player.UserId
	local banned
	
	if manualbanned[userid] then
		player:Kick("You have been banned by a Developer")
	end
	
	local success, errormessage = pcall(function()
		local playeruserid = player.UserId
		banned = bds:GetAsync(playeruserid)
	end)
	
	if banned then
		player:Kick("You have been banned by an Administrator")
	end
end)

function execCommand(cmd, plr)
	--// Kill Function \\--
	local args = cmd:split(" ")
	if args[1] == prefix.."kill" then
		if game.Players:FindFirstChild(args[2]) then
			game.Players:FindFirstChild(args[2]).Character.Humanoid.Health = 0
		else
			setoutput(plr, "Player Not Found, "..tostring(args[2]))	
		end
		
	--// Speed Function \\--	
	elseif args[1] == prefix.."speed" then
		if game.Players:FindFirstChild(args[2]) then
			if args[3] then
				game.Players:FindFirstChild(args[2]).Character.Humanoid.Speed = tonumber(args[3])
			else
				setoutput(plr, "Invalid Argument, "..tostring(args[3]))	
			end
		else
			setoutput(plr, "Player Not Found, "..tostring(args[2]))	
		end
		
	--// Jump Function \\--	
	elseif args[1] == prefix.."jump" then
		if game.Players:FindFirstChild(args[2]) then
			if args[3] then
				game.Players:FindFirstChild(args[2]).Character.Humanoid.JumpPower = tonumber(args[3])
			else
				setoutput(plr, "Invalid Argument, "..tostring(args[3]))
			end
		else
			setoutput(plr, "Player Not Found, "..tostring(args[2]))	
		end
		
	--// Bring Function \\--	
		elseif args[1] == prefix.."bring" then
			if game.Players:FindFirstChild(args[2]) then
				game.Players:FindFirstChild(args[2]).Character.HumanoidRootPart.CFrame = plr.Character.HumanoidRootPart.CFrame + Vector3.new(0, 2, 0)
		else
			setoutput(plr, "Player Not Found, "..tostring(args[2]))	
		end
		
	--// Goto Function \\--	
		elseif args[1] == prefix.."goto" then
			if game.Players:FindFirstChild(args[2]) then
			plr.Character.HumanoidRootPart.CFrame = game.Players:FindFirstChild(args[2]).Character.HumanoidRootPart.CFrame + Vector3.new(0, 2, 0)
		else
			setoutput(plr, "Player Not Found, "..tostring(args[2]))
		end
		
	--// Jail Function \\--
		elseif args[1] == prefix.."jail" then
			if game.Players:FindFirstChild(args[2]) then
				game.Players:FindFirstChild(args[2]).Character.HumanoidRootPart.CFrame = game.Workspace.JailTeleport.CFrame
		else
			setoutput(plr, "Player Not Found, "..tostring(args[2]))		
		end
		
	--// Unjail Function \\--
		elseif args[1] == prefix.."unjail" then
			if game.Players:FindFirstChild(args[2]) then
				game.Players:FindFirstChild(args[2]).Character.HumanoidRootPart.CFrame = game.Workspace.UnjailTeleport.CFrame
		else
			setoutput(plr, "Player Not Found, "..tostring(args[2]))		
		end
		
	--// Kick Function \\--
		elseif args[1] == prefix.."kick" then
			if game.Players:FindFirstChild(args[2]) then
				game.Players:FindFirstChild(args[2]):Kick("An Administrator has kicked you for the for the following "..tostring(args[3]))
		end	
	end
end

2 Likes

Try printing out args[2]. Does it print out the username?

1 Like

Yes, it prints out the username as args[2] is the players name, and the players name is usually not separated with a space.

1 Like

You’re splitting the string by spaces, so the third argument is only the third word of your chat message. You’ll want to cut the original message so that only the text after the first two arguments remains.

The way you’ve set this up makes it quite easy - you don’t modify the ‘cmd’ value at all - so you could do the following:

local kickMessage = cmd:sub(#args[1] + #args[2] + 3)

Those aren’t magic numbers either; you want to skip however many characters are in args[1] and args[2], plus two spaces. Since string.sub takes the position of the first character as an argument, you’ll want to add 1 to the length of the entire string you’re skipping to get the correct position of the first character.

4 Likes

Tricky code. Mostly I go trial and error at this moment. I’ll try my best to cover you with possible solutions.

EDIT: Ah, the guy above probably found the solution.

I get what you’re trying to say here but what do you mean by "add 1 to the length of the entire string you're skipping to get the correct position"?

The best way to explain this is probably with an example. Say you have the string “Hello World” and want to cut out the word “Hello” and the space that follows it. You might be inclined to do the following:

local world = ("Hello World"):sub(6) --The length of "Hello" plus a space

However, that copies everything starting at the 6th character into the new string, which would include the space before “World”. You have to add 1 to the length of the string you’re wanting to cut out, because the value you pass to string.sub is the position of the first character you want copied. So, the correct way to do this would be:

local world = ("Hello World"):sub(7) --The length of "Hello" plus a space plus 1

Hopefully this makes more sense!

1 Like

Ahh yes, I see now, thank you, so it basically just retrieves the rest of the text past 7 or so numbers/letters in length.

1 Like

Hello,

I’ve run into this problem before, but found you can utilize table.concat to get the rest of the words in your string table.

local reason = table.concat(args, " ", 3)

Would get all the words at and after args[3], assuming args[1] is the command, and args[2] is the target player.

Hope this helps!

3 Likes

Nevermind, I am so dumb, I accidentally put this on my backup script which was not connected to the remote event that’s fired, it works perfectly fine, so thank you so much.

1 Like