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

how do i combine 3 message:sub?

I have spent a few hours to figure how to make a command like /setmoney player 100 Here is how my code looks like now(Its my latest code for trying this):

admins = {" ", " ", " "}

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		for i, adminName in pairs(admins) do
			if player.Name == adminName then
				if message:sub(1, 10) == "/setmoney " then
					local y = tonumber(message:sub(12))
					local targetPlayer = game.Players:FindFirstChild(message:sub(11))
						de = targetPlayer.leaderstats.Money.Value
						de = y
					
				end
			end
		end
	end)
end)

Im able to make like /freeze, /kill, /kick, /ban etc...

But when i try to make a third message sub to my command it never works. I know why the code about doesnt work cause im trying to put a message:sub after a message:sub that doesnt have a end. In this case im trying to put a number at the 12 letter. But i dont know how long the players name will be. Im also having trouble with /kick player "reason" but i think i can figure that out if i get help with these.

Feel free to rewrite my entire code if you found a solution.

Summary: Im trying to add a third message sub to /setmoney player so i can type /setmoney player 100 100 is just an example i want to be able to set a random rumber (not math.random) XD

(sry for my bad english :3)

if you are curious heres my kick command that works (without a reason)

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		for i, adminName in pairs(admins) do
			if player.Name == adminName then
				if message:sub(1, 6) == "/kick " then
					local targetPlayer = game.Players:FindFirstChild(message:sub(7))
					if targetPlayer then
						targetPlayer:Kick("You've been kicked by" .. player.Name)
						break
					end
				end
			end
		end
	end)
	
end)

1 answer

✓ Accepted answer

The way you're doing it is not the best way, for admin commands you should take a look at string patterns as they're extremely usefull

The best way I found is to get all words in a table

Example

local function GetWords(sentence)
	local Words = {}
	for word in string.gmatch(sentence, '%S+') do -- String patterns..
		table.insert(Words, word)
	end
	return Words
end

now you can have any amount of args you want without having to count or so

local Admins = {' ', ' ', ' '} -- Names can, but I recommend userIds
function isAdmin(plr)
	for i,v in pairs(Admins) do
		if plr.Name == v then
			return true
		end
	end
	return false -- This is possible because after a return, the code is 'stopped'
end

local function GetWords(sentence)
	local Words = {}
	for word in string.gmatch(sentence, '%S+') do
		table.insert(Words, word)
	end
	return Words
end

local function GetTarget(PlrName)
	for index, plr in pairs(game.Players:GetPlayers()) do
		if plr.Name == PlrName then
			return plr
		end
	end
	for index, plr in pairs(game.Players:GetPlayers()) do
		if plr.Name:lower():sub(1,PlrName:len()) == PlrName:lower() then -- Some string manipulation
			return plr
		end
	end
end

game.Players.PlayerAdded:Connect(function(plr)
	if isAdmin(plr) then
		plr.Chatted:Connect(function(message)
			local MsgWords = GetWords(message)

			if MsgWords[1] == '/setmoney' then
				local Target = GetTarget(MsgWords[2]) or plr -- if not found, use the player who wrote the message
				local Amount = tonumber(MsgWords[3]) or 1000

				-- Set money to amount
			end
		end)
	end
end)

Of course you can also fix your code but this way is better

If you have a question/I made a typo or so, just ask

EDIT: the reason your code for kick worked, is because :sub(7) starts from the 7th character, but if you would type '/kick plr something' it would fail as you don't know how long the name is that he typed

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