【发布时间】:2021-07-20 00:57:02
【问题描述】:
我正在尝试创建一个插件,在用户在 roblox studio 上玩游戏之前从服务器获取附加代码。
基本上,用户将使用类似 blockly 的东西在网站上创建 luau 代码,我想将该代码发送到 roblox studio。我见过一些插件不时从服务器获取新数据,我已经能够做到这一点,但我想看看是否有办法只在用户点击播放时获取新代码按钮,因为每 5 秒左右请求一次新数据可能会很昂贵。
下面是一个简单的插件,它会在游戏加载时尝试向服务器发送请求,但脚本永远不会超出game.Loaded:Wait()
主文件:
local Request = require(script.Parent.Request)
local URL = "http://localhost:3333"
local toolbar = plugin:CreateToolbar("Test")
local button = toolbar:CreateButton("Test", "Test", "rbxassetid://4458901886")
local isListening = false
local request = Request.new()
local ok
local json
local function onClick ()
isListening = not isListening
if (isListening == false) then
return print("Not listening")
end
print("Listening")
if not game:IsLoaded() then
print(game.Loaded)
game.Loaded:Wait()
print("Game has started")
ok, json = request:Get(URL)
print(ok, json)
end
end
button.Click:Connect(onClick)
请求文件:
local Request = {}
Request.__index = Request
function Request.new()
return setmetatable({}, Request)
end
function Request:Get(URL)
local ok, result = pcall(game.HttpService.GetAsync, game.HttpService, URL)
local json = game.HttpService:JSONDecode(result)
return ok, json
end
return Request
【问题讨论】: