Search code examples
variableslualua-tablehttpserviceroblox

Rbx.Lua - Why can't I store a .txt file as a table?


I have a python-based web server running on my raspberry pi that web scrapes trade currency rates on Roblox. If you don't know what I just said, all you need to know is that I am gathering numbers that change on a certain webpage. I want to import this collected information into my Roblox game so I can graph it (I already have the grapher made).

Here is what I did to import it:

bux = game.HttpService:GetAsync("http://tcserver.raspctl.com:5000/AvailableRobux.txt")
bux = {bux}
tix = game.HttpService:GetAsync("http://tcserver.raspctl.com:5000/AvailableTickets.txt")
tix = {tix}

This gives me a 404 response. If I access the web server from my computer (On the same network), it also gives me a 404 response. I know I am port-forwarding correctly because the following line of lua DOES work.

print(game.HttpService:GetAsync("http://tcserver.raspctl.com:5000/AvailableRobux.txt"))

I need the robux and ticket rates to be stored in a table. Go to one of the URLs with the rate data and you will see that it is already formatted for a Rbx.Lua table, it just needs the curly braces. How can I turn my data into a table?


Solution

  • You can't just convert a string to a table like that, you need to split the components into a table by splitting it along the delimiters (the commas). See this page about splitting strings in Lua. I would suggest taking out the spaces and only having commas between the data entries.

    Here's an example of what you'll need. The explode function is from the link I posted. I have not tested it.

    function explode(d,p)
      local t, ll
      t={}
      ll=0
      if(#p == 1) then return {p} end
        while true do
          l=string.find(p,d,ll,true) -- find the next d in the string
          if l~=nil then -- if "not not" found then..
            table.insert(t, string.sub(p,ll,l-1)) -- Save it in our array.
            ll=l+1 -- save just after where we found it for searching next time.
          else
            table.insert(t, string.sub(p,ll)) -- Save what's left in our array.
            break -- Break at end, as it should be, according to the lua manual.
          end
        end
      return t
    end
    
    bux = game.HttpService:GetAsync("http://tcserver.raspctl.com:5000/AvailableRobux.txt")
    bux = explode(",",bux)
    tix = game.HttpService:GetAsync("http://tcserver.raspctl.com:5000/AvailableTickets.txt")
    tix = explode(",",tix)