Jump to content




Seperating A String Into Multiple Variables


  • You cannot reply to this topic
6 replies to this topic

#1 Inumel

  • Members
  • 120 posts

Posted 18 November 2013 - 04:35 PM

I am planning on making a Chat based teleport system with miscperipherals chatbox and a command block, so all the user has to say is "Teleport to playername" and it will teleport them to said player

I wish to get the playername they want to teleport out of that string, and need a bit of help doing it

I tried putting it into a table first, IE
a = "teleport to playername"
tab = {}
table.insert(tab,a)
print(tab[1],tab[2],tab[3])
Sadly this only put the entire string on tab[1](which i honestly expected)

So can anyone tell me how to seperate the string by word and insert it into a table?

thanks!

#2 TheOddByte

    Lazy Coder

  • Members
  • 1,607 posts
  • LocationSweden

Posted 18 November 2013 - 04:37 PM

Well you should look into string.gsub and patterns that is on the next page of the link I posted.

#3 Inumel

  • Members
  • 120 posts

Posted 18 November 2013 - 04:39 PM

Ill do that! thank you

#4 Inumel

  • Members
  • 120 posts

Posted 18 November 2013 - 04:49 PM

Extremely confusing! no wonder i havent messed with gsub before :s

#5 Inumel

  • Members
  • 120 posts

Posted 18 November 2013 - 05:25 PM

So yeah, after reading this page over twice i still cant figure it out.. can you perhaps give me an example? or the solution to my issue? i apologize if thats a problem but i simply cannot figure it out

#6 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 18 November 2013 - 05:51 PM

There are other ways, but I'd do:

a = "teleport to playername"
if a:sub(1,12):lower() == "teleport to " then
  print("I'm gonna teleport you to "..a:sub(13,#a))
end


#7 Inumel

  • Members
  • 120 posts

Posted 18 November 2013 - 05:58 PM

Works perfectly, edited for my specific purpos:
local evt, player, message = os.pullEvent("chat")
if message:sub(1,12):lower() == "teleport to " then
  print(message:sub(13,#message))
end
Thank you very much for the help, saved me a lot of hassle!





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users