我有一个基于 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 和票价存储在一个表中。转到带有速率数据的 URL 之一,您会看到它已经为 Rbx.Lua 表格式化,它只需要大括号。如何将我的数据转换为表格?
最佳答案
您不能像那样将字符串转换为表格,您需要通过沿分隔符(逗号)将组件拆分成表格。 See this page about splitting strings in Lua .我建议去掉空格并在数据条目之间只使用逗号。
这是您需要的示例。爆炸功能来 self 发布的链接。我没有测试过。
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)
关于variables - Rbx.Lua - 为什么我不能将 .txt 文件存储为表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29065491/