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

[String.sub] Why would this NOT be subing???

Im getting no output for Lua.. and the function also wont run. This is V2 so... I know the way I have the admin set up it SHOULD run it, but yet fails.

BTW, I learned string.sub yestraday.. Please do not judge if this is bad coding. :L

AddCommand(0,"Print","Print","P",":Print I like tacos --> [M.A.C.] I like tacos",function(Speaker,Msg)
	_G.Print(Speaker..' : '..Msg)
end) -- ^^^^ This will insert into _G.Commands.

CheckChat=function(plr,msg)
	table.foreach(_G.Commands,function(i,v)
		local Cmd=v.Cmd:lower()
		local Cmd2=v.Cmd2:lower()
		local SubCount=1
		if msg:sub(1,3):find('/e ') then 
			SubCount=3
		else 
			SubCount=1 
		end
		_G.Print(msg)
		if msg:sub(SubCount,#( (Settings.FrontPrefix..(Cmd) or Settings.FrontPrefix..(Cmd)..Settings.MiddlePrefix) 
		or (Settings.FrontPrefix..(Cmd2) or Settings.FrontPrefix..(Cmd2)..Settings.MiddlePrefix) )) 
		==
		((Settings.FrontPrefix..(Cmd) or Settings.FrontPrefix..(Cmd)..Settings.MiddlePrefix) 
		or (Settings.FrontPrefix..(Cmd2) or Settings.FrontPrefix..(Cmd2)..Settings.MiddlePrefix) ) then
			if (_G.GetRank(plr) <= v.Rank) then
				msg=msg:sub(#((Settings.FrontPrefix..(Cmd) or Settings.FrontPrefix..(Cmd)..Settings.MiddlePrefix) 
				or (Settings.FrontPrefix..(Cmd2) or Settings.FrontPrefix..(Cmd2)..Settings.MiddlePrefix))+1)
				local r,e=ypcall(function()
					v.Func(plr,msg)
				end)
				if not r then _G.Error(e) end
			else
				--You cant acess this command!
			end
		else
			--Sorry command was not found!
		end
	end)
end

1 comment

0

Sorry if you gonna help you kinda HAVE to read all the string.sub... :L

1 answer

✓ Accepted answer

You should definitely make this much simpler. Advanced is not the same thing as complicated. This script is complicated, not advanced.

Here's a list of things to make it better:

  • foreach is deprecated. It's also a lot more confusing than just using pairs.
  • CheckChat = function should be function CheckChat
  • i is unused, name it _
  • SubCount is a poor name. Maybe call it start or something meaningful
  • if msg:sub(1, 2):find("/e") is the same thing as if msg:sub(1, 2) == "/e"
  • You say Settings. 24 times. Make variables!
  • Every single time Cmd and Cmd2 are used, you first add front to them. Just change the variable instead.

Here's some style things you should do:

  • Pick one type of quote, don't use both ' and ". " is usually the preference (because of precedent from other languages)
  • Put spaces around your operators and other symbols, especially .. and =. It makes it read so much easier, and you can even add extra spaces in places to indicate what things should be grouped
  • Don't use the print "msg" style of calls, it's very inconsistent with the rest of Lua
  • No point in wrapping things in parens, e.g, Cmd2. You don't need to when you have spaces (see above)
  • Break long statements up. There's no point in having a line that does a ton of things if you can't understand it.
  • or is associative, and (usually) commutative, so it doesn't make sense to put parentheses around these things.

Here is what I got:

function CheckChat(plr,msg)
	for _, v in pairs(_G.Commands) do
		local Cmd = v.Cmd:lower()
		local Cmd2 = v.Cmd2:lower()
		local start = 1
		if msg:sub(1, 2) == "/e" then 
			start = 3
		else 
			start = 1 
		end

		local mid, front = Settings.MiddlePrefix, Settings.FrontPrefix;
		Cmd = front .. Cmd
		Cmd2 = front .. Cmd2

		-- This doesn't make sense:
		local partOfMsg = msg:sub(start, #( (Cmd .. mid or Cmd ) or (Cmd2 .. mid or Cmd2) ))
		-- Neither does this:
		local commandCheck = (Cmd .. mid or Cmd) or (Cmd2 .. mid or Cmd2)

		if partOfMsg == commandCheck then
			print('got bypass msg:sub')
			if _G.GetRank(plr) <= v.Rank then
				print('got by getrank')

				-- This doesn't make sense:
				local before = (Cmd .. mid or Cmd) 
					or (Cmd2 .. mid or Cmd2) 
					or ((Cmd .. mid or Cmd) 
					or (Cmd2 .. mid or Cmd2))

				msg = msg:sub(#before + 1)
				local r, e = ypcall(function()
					v.Func(plr, msg)
				end)
				if not r then _G.Error(e) end
			else
				--You cant acess this command!
			end
		else
			--Sorry command was not found!
		end
	end
end

Now that we've done all this, there's a ton of stuff that makes no sense.

-- This:
#( (Cmd .. mid or Cmd )  or (Cmd2 .. mid or Cmd2) )
-- Is equivalent to this:
# ( Cmd .. mid )

so I have no idea what you were going after with the remainder of the script and all of the ors.


You cannot say if a == b or c. The precedence will make this if (a == b) or c, which is effectively just if true. Expressions cannot have "two values" at the same time. That's not what or does.

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