generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

string.format and string.match?

Previous Thread :: Next Thread 
JimmyChance is not online. JimmyChance
Joined: 01 Nov 2009
Total Posts: 3681
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
ForeverDev is not online. ForeverDev
Joined: 04 Oct 2008
Total Posts: 13300
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 is not online. 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
ForeverDev is not online. ForeverDev
Joined: 04 Oct 2008
Total Posts: 13300
12 May 2014 09:36 PM
^ I had a brain fart.
Report Abuse
JimmyChance is not online. JimmyChance
Joined: 01 Nov 2009
Total Posts: 3681
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
CeaselessSoul is not online. CeaselessSoul
Joined: 03 Jul 2012
Total Posts: 7506
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
JimmyChance is not online. JimmyChance
Joined: 01 Nov 2009
Total Posts: 3681
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 is not online. transIate
Joined: 20 Jun 2013
Total Posts: 2699
12 May 2014 10:07 PM
well duh you're returning table values
Report Abuse
nighttimeninja314 is not online. nighttimeninja314
Joined: 04 Apr 2011
Total Posts: 4001
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 is not online. 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
islandmaker2012 is not online. islandmaker2012
Joined: 07 Nov 2012
Total Posts: 9327
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 is not online. 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
islandmaker2012 is not online. islandmaker2012
Joined: 07 Nov 2012
Total Posts: 9327
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 is not online. 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
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image