Rbx.Lua - 为什么我不能将 .txt 文件存储为 table?

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

我的 raspberry pi 上有一个基于 python 的网络服务器 运行,该网络在 Roblox 上抓取交易货币汇率。如果你不知道我刚才说的是什么,你只需要知道我正在收集某个网页上变化的数字。我想将这些收集到的信息导入到我的 Roblox 游戏中,以便我可以绘制它(我已经制作了绘图器)。

这是我导入它的方法:

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}

这给了我 404 响应。如果我从我的计算机(在同一网络上)访问网络服务器,它也会给我一个 404 响应。我知道我的端口转发是正确的,因为以下行 lua 确实有效。

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

我需要将 robux 和票价存储在 table 中。转到包含比率数据的 URL 之一,您会看到它已经被格式化为 Rbx.Lua table,它只需要花括号。如何将我的数据变成 table?

您不能像那样将字符串转换为 table,您需要通过沿分隔符(逗号)将组件拆分为 table。 See this page about splitting strings in Lua。我建议去掉空格并在数据条目之间只使用逗号。

这是您需要的示例。 explode 函数来自我发布的link。我没有测试过。

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)