Search code examples
jsonserverluaroblox

I am struggling to discover a good method to link my server to my Roblox game


Yesterday I set up an Ubuntu server to run some out of game scripts to work in conjunction with in game scripts. I enabled HTML calls as well as Third Party access in my game editor. I have also looked through the object browser for possible objects to use in the get and post requests. I have come up with some code, but it is completely nonfunctional.

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()
    
Any advice or help would be appreciated. Thanks.

Solution

  • First, enable HTTP if you haven't already: enter image description here

    iirc, you cannot send http requests from the client: you'll have to use a server script. This means that you cannot get the player via LocalPlayer. I've provided an example of a POST request which uses JSON to send data.

    Script in 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)
    

    I don't know how your server is structured so you may have to work around this. There's a wonderful wiki page that has more information and examples.