HTTP Service with Amazon Lightsail

I made an in-game group system where players can create groups, invite other players, kick other players, create roles within their group, assign roles, etc. I was storing this all under a datastore, but I decided to store it using HTTP Service. I have no experience with web development and I haven’t found much online about how to setup the back end stuff.

I have a lightsail server and setup a database. I accessed it with MySQL using the username, password, and endpoint. I’m just very confused on how I actually access this from ROBLOX.

I wasn’t able to find any tutorials on what programming needs to be done on the database. Any help on this topic is appreciated.

1 Like

Why not use Roblox datastores or datastore 2 ?

If you really want to use SQL you could send an HTTP GET request to your website using HttpService. https://developer.roblox.com/en-us/api-reference/class/HttpService

DataStores are fairly limited in their capability. Notice that OP switched from them to a custom solution. Suggesting to change back isn’t really the brightest choice.

1 Like

I understand the actual HTTP service on roblox, I’m confused about the server and what programming needs to be done there to communicate with roblox.

Oh, just remembered you had no web experience.

You could use the httpservice JSON Encode function on a dictionary then use a HTTP post request to your lightsail website thing. I don’t really know what light sail is, so I can’t help you too much outside of roblox.

If you server supports .php files, you would need a couple of them:
Note in the php files, comments I add for comprehension will be marked with <-, so delete them before using or this won’t work

This one would be for adding data to the table
$servername = "localhost";
$username = "sqldatabaseusername";
$password = "sqldatabasepassword";
$dbname = "sqldatabasename"; <- This and the 3 lines above it are all the credentials for your SQL database

$key = "Put something here so people can't randomly access your data";
$rest_json = file_get_contents("php://input"); <- Only put this in if using $_POST alone is not working for you
$_POST = json_decode($rest_json, true); <- same with this one
if ($_POST['key'] ==$key){
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}  <- Attempts to connect to the SQL database and will error out if it does not work

$data = $_POST['datatable']; <- with datatable here, you would change it to whatever you are titling what you send with POST from Roblox
$sql = "INSERT INTO PUTTABLENAMEHERE (data) <- puttablenamehere would be the name of a table you have in the database, such as USER_DATA
VALUES ($data)";

if ($conn->query($sql) === TRUE) {
    $last_id = $conn->insert_id;
    echo "New record created successfully. Last inserted ID is: " . $last_id;
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
} <- Tells you if it works or not

$conn->close(); <- Kills the connection for a few reasons
}
?>
This one would be to get data from the server
$servername = "localhost";
$username = "sqldatabaseusername";
$password = "sqldatabasepassword";
$dbname = "sqldatabasename"; 

$key = "Put something here so people can't randomly access your data";
$rest_json = file_get_contents("php://input"); <- AGAIN: Only put this in if using $_POST alone is not working for you
$_POST = json_decode($rest_json, true); <- same with this one
if ($_POST['key'] ==$key){
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}  
$resultstable = array();
$query="SELECT COLUMN FROM DATATABLE "; <- Replace COLUMN with the column in your database you want to retrieve, you could send through http from roblox what column you want, if you did that you would put the following under the $resultstable array: $datacolumn = $_POST["datacolumn"]; OR you could put * and get all matching results and you would to the same with datatable except you could not use *
$results =mysqli_query($mysqli,$query,MYSQLI_USE_RESULT);
while ($row = $results->fetch_assoc()) {
array_unshift($resultstable,$row);
};
echo json_encode($resultstable);
$conn->close();
}
?>
Now for the roblox side:
local tabletosend = {
	key = 'Your super secure key from earlier',
	whatever you had as your $data = $_POST['datatable'] = datatosend
}

HttpService:PostAsync("https://averygooddomainname.com/epicscriptname.php",game.HttpService:JSONEncode(tabletosend),Enum.HttpContentType.ApplicationJson,false)

The above script would work for both sending and receiving data, to see what the server sent, just assign the HttpService request to a variable or encapsulate it with print().

Please note, all of the above assumes your server uses at least PHP version 7.1.
If the POST request is not working for you, add back in the lines:

$rest_json = file_get_contents("php://input"); 
$_POST = json_decode($rest_json, true); 

But test the scripts without them first, as they are not the recommended way to use $_POST.
Sorry if this is a little unclear, I sort of rushed this a bit.

Have you considered using RBXMod? It is designed to make the process of running Lua code remotely very easy. Here is the docs.

This was a able to solve my problems.

1 Like

Sadly I can’t get the tutorial approved. :frowning:

1 Like