|
| 12 May 2014 09:25 PM |
| When is the best time to use these? Also, where can I read into in-depth(I didn't really grasp the concept in the Wiki's function dump). |
|
|
| Report Abuse |
|
|
|
| 12 May 2014 09:34 PM |
I sometimes use string.format for printing out lots of data. For example:
local num = 2 local power = 4
print(string.format( "%s to the power of %s is equal to %s", tostring(num), tostring(power), tostring(num^power) ))
>> 2 to the power of 4 is equal to 16
string.match looks for patterns. I usually use it to try to take numbers out of a string. For example:
print(string.match("lol5", "%d"))
>>5
(I could have been wrong on the string.match, because I don't use it very often) |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 12 May 2014 09:35 PM |
Forever, why not just do:
local num = 2 local power = 4
print(string.format( "%d to the power of %d is equal to %d", num, power, num^power ))
|
|
|
| Report Abuse |
|
|
| |
|
|
| 12 May 2014 09:38 PM |
names = {"Player%d"}
local check = function(name) for _, n in pairs(names) do if name:match() == n then return _ end end end
local player = function(player) if check(player.Name) then print (player.Name .. " is an admin!") else print (player.Name .. " is not an admin!") end end
game.Players.PlayerAdded:connect(player)
Why does this not print "Player1" is an admin in the output? Instead, it errors with :
21:36:42.602 - Workspace.Script:5: bad argument #1 to 'match' (string expected, got no value) 21:36:42.603 - Script 'Workspace.Script', Line 5 - upvalue check 21:36:42.604 - Script 'Workspace.Script', Line 12 21:36:42.605 - Stack End |
|
|
| Report Abuse |
|
|
|
| 12 May 2014 09:59 PM |
_ is the index, no good, you need the value:
function check(plr) for i,v in pairs(game.Players:GetPlayers()) do if string.lower(v.Name:sub(1,#plr)) == plr:lower() then return v -- the value, not the index end end end
CeaSoul CeaSoul by the Cea Sore |
|
|
| Report Abuse |
|
|
|
| 12 May 2014 10:00 PM |
^
I "cea" what you did there.
Why does this work, even when I return the index?
names = {"Player%d"}
local check = function(name) for _, n in pairs(names) do if name:match(n) then return _ end end end
local player = function(player) if check(player.Name) then print (player.Name .. " is a match!") else print (player.Name .. " is not a match!") end end
game.Players.PlayerAdded:connect(player) |
|
|
| Report Abuse |
|
|
transIate
|
  |
| Joined: 20 Jun 2013 |
| Total Posts: 2699 |
|
|
| 12 May 2014 10:07 PM |
| well duh you're returning table values |
|
|
| Report Abuse |
|
|
|
| 12 May 2014 10:38 PM |
" I didn't really grasp the concept in the Wiki's function dump"
Firmly grasp it. Nuff said. |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 12 May 2014 10:40 PM |
| It doesn't matter what you return, as long as you return something not false or nil. You might as well return workspace since it's not a false or nil value |
|
|
| Report Abuse |
|
|
|
| 12 May 2014 10:42 PM |
names = {"Player"}--why do you need the "%"??????
local check = function(name) for _, n in pairs(names) do if name:match(n) then return true end end
return false end
local player = function(player) if check(player.Name) then print (player.Name .. " is a match!") else print (player.Name .. " is not a match!") end end
game.Players.PlayerAdded:connect(player)
|
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 12 May 2014 10:44 PM |
| The main point of the percent sign was to use it for string.match. |
|
|
| Report Abuse |
|
|
|
| 12 May 2014 11:08 PM |
but isn't that in string.gmatch?
because ive used string.match..
if string.match(String1,String2) then |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 12 May 2014 11:18 PM |
It can be used in both, you don't have to use it. It can even be used in gsub a\ |
|
|
| Report Abuse |
|
|