How to unpack a string?

Hello, I am trying to make an admin command script where you can type something like ;s print'hi' and It would print to the console. The problem is that the string would just show up as nothing even when I print it. (Also I do have LoadStringEnabled) Heres my code:

local Commands = {
	[";kick"] = {Type = 2, Function = function(Message, Reason)
		local Player = FindPlayerFromMessage(Message)
		Player:Kick(Reason)
	end};
	
	[";s"] = {Type = 5, Function = function(Message)
		local s, e = pcall(function()
			assert(loadstring(Message))()
		end)
		if not s then warn(e) end
	end};
}
game.Players.PlayerAdded:Connect(function(Player)
Player.Chatted:Connect(function(Message)
if GroupRank >= 254 then 
			Message = string.split(Message, " ")
			local Command = Message[1]
			local Parameter = Message[2]
			local Parameter2 = Message[3]
			if Commands[Command] then
				if Commands[Command].Type == 1 then
					local s, e = pcall(function() Commands[Command].Function(Parameter) end)
					if not s then warn(e) end
				elseif Commands[Command].Type == 2 then
					local s, e = pcall(function() Commands[Command].Function(Parameter, Parameter2) end)
					if not s then warn(e) end
				elseif Commands[Command].Type == 3 then
					local s, e = pcall(function() Commands[Command].Function(Player, Parameter, Parameter2) end)
					if not s then warn(e) end
				elseif Commands[Command].Type == 4 then
					local s, e = pcall(function() Commands[Command].Function(Player) end)
					if not s then warn(e) end
				elseif Commands[Command].Type == 5 then
					local new = unpack(Message) --problem is here
					print(string.sub(new, 4)) -- It would just print "" an empty string
					local s, e = pcall(function() Commands[Command].Function(string.sub(new, 4)) end)
					if not s then warn(e) end
				end
			end
end)
end)

Can anyone help? Thanks!

You could do something like this, assuming that args is the table from the string.split function.

local unpackedarguments
for _,v in pairs (args) do
    unpackedarguments=unpackedarguments.." "..v
end

If you wanted to not include the ;s part of the command, you could just set Args[1] to nil before running the loop above.

1 Like

Please try to isolate the issue down as much as possible – don’t just give us code and say “Can anyone help?”

The issue here is that you’re trying to do something that doesn’t make sense. To clarify, string.split(Message, " ") creates a table that looks something like this: {";s", "print'hi'"}. Your code is unpacking that table (which in this case returns ";s", "print'hi'") and assigning that to new. Meaning, you’re basically doing this: local new = ";s", "print'hi'".

That obviously is just assigning new to be ";s". You’re then trying to get a string that’s every character of ;s from position 4 and up. There are only two characters in ;s, so this just returns an empty string.

You’ll can use a built-in function (table.concat) to replace new. You can learn more about table.concat here, but basically you’ll want to replace string.sub(new, 4) with table.concat(Message, " ", 2), which will make a string out of Message starting from the second index.

3 Likes