我正在苦苦寻找将服务器连接到我的 Roblox 游戏的好方法

昨天我设置了一个 Ubuntu 服务器,运行一些游戏外的脚本,并与游戏内脚本协同工作。我在游戏编辑器中启用了 HTML 调用以及第三方访问。我还查看了对象浏览器,寻找可能用于获取和发布请求的对象。我想出了一些代码,但它完全无效。

local UserId = game.Players.LocalPlayer
local mining = UrlEncode("http://216.128.0.0:34648/")

for mining in Connect(function()




    getAsync(mining.."/"..UserId)
    PostAsync(mining.."/"..UserId)
    end)

     do JSONEncode()

    JSONDecode()

任何建议或帮助将不胜感激。谢谢。

原文链接 https://stackoverflow.com/questions/70284349

点赞
stackoverflow用户17060604
stackoverflow用户17060604

首先,如果您尚未启用HTTP,请启用它: enter image description here

如果我没记错的话,您无法从客户端发送HTTP请求:您必须使用服务器脚本。这意味着您无法通过 LocalPlayer 获取玩家。 我提供了一个使用 JSON 发送数据的 POST 请求 的示例。

ServerScriptService 中的脚本:

local HttpService = game:GetService("HttpService")
local serverURL = "http://216.128.0.0:34648/mining"

function mine(player)
    local arguments = {
        ["userID"] = player.UserId,
        ["name"] = player.Name
    }

    local response = HttpService:PostAsync(serverURL, HttpService:JSONEncode(arguments), Enum.HttpContentType.ApplicationJson)
    local responseData = HttpService:JSONDecode(response) --assuming the response is in JSON
    print(responseData)
end

game.Players.PlayerAdded:Connect(mine)

我不知道您的服务器结构是如何的,所以您可能需要进行相应的调整。有一个精彩的 维基页面 提供了更多信息和示例。

2021-12-09 08:36:43