Hey, I want to have my own DataStore in MySQL.
I already have my MySQL Database working with a PHP script that can Read or Add Rows into the Table.
The problem is that I don’t know how to interact with it by using Roblox… Some people told me to use HTTPService , but what exactly will be sent to my PHP page?
This is my current Roblox Script :
local HttpService = game:GetService("HttpService")
game.Players.PlayerAdded:Connect(function(player)
local playerUserId = "Player_" .. player.UserId
local success = pcall(function()
HttpService:PostAsync("Website", "PlayerID="..playerUserId)
end)
if not success then
print("The Remote server could not be accessed.")
else
print("Sent "..playerUserId.." to Database")
end
end)
local HttpService = game:GetService("HttpService")
game.Players.PlayerAdded:Connect(function(player)
local playerUserId = "Player_" .. player.UserId
local success = pcall(function()
print(HttpService:PostAsync("Website", "PlayerID="..playerUserId))
end)
if not success then
print("The Remote server could not be accessed.")
else
print("Sent "..playerUserId.." to Database")
end
end)
Correct me if I’m wrong but the MySQL database you get with 000webhost runs on its own separate server, not localhost (which would be your website’s server)? Double check your SQL connection details.
I’d also recommend securing your web server with some form of authentication, e.g. an API key. There’s nothing stopping me making a POST request to update your database.
You should be sending a JSON-encoded payload to your server script from Roblox using JSONEncode. If you’re using Php, you must get the raw input like this
local HttpService = game:GetService("HttpService")
game.Players.PlayerAdded:Connect(function(player)
local playerUserId = "Player_" .. player.UserId
local Data = {
["PlayerID"] = playerUserId;
}
Data = HttpService:JSONEncode(Data)
local success = pcall(function()
print(HttpService:PostAsync("WebPage", Data))
end)
if not success then
print("The Remote server could not be accessed.")
else
print("Sent "..playerUserId.." to Database")
end
end)
But how do I extract my PlayerID from the $Payload variable?
Okay thanks MrNicNac,
sadly my Database is still Empty. I’m probably still doing something wrong.
But I have no more PHP error when i’m launching my Roblox game.
Roblox Script :
local HttpService = game:GetService("HttpService")
game.Players.PlayerAdded:Connect(function(player)
local playerUserId = "Player_" .. player.UserId
local Data = {
["PlayerID"] = playerUserId;
}
Data = HttpService:JSONEncode(Data)
local success = pcall(function()
print(HttpService:PostAsync("WebPage", Data))
end)
if not success then
print("The Remote server could not be accessed.")
else
print("Sent "..playerUserId.." to Database")
end
end)
Well, unfortunately I’m not magic and can’t just know the issue by staring at the script. We’ll need some output data about what is happening.
PostAsync returns the response from the server (Php) language. You should be capturing this, which it seems you are. Does it just tell you 0 results?
Starting here in your Php script
if($PlayerID != NULL){
Things just start to get a bit… wonky. Try doing this approach instead.
<?php
$servername = "localhost";
$username = "username ";
$password = "password";
$dbname = "dbname ";
// Connect to MySQLI and ask it to throw exceptions
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
try{
$conn = new mysqli($servername, $username, $password, $dbname);
}catch(mysqli_sql_exception $e){
print("Database connection error: " . $e->getMessage());
}
// Fetch the payload from Roblox
$payload = json_decode(file_get_contents("php://input"), true);
if (empty($payload)){
print("Payload from Roblox is empty. No data was received.");
}else{
$playerID = $payload['PlayerID'];
if (isset($playerID)){
// Insert a new player ID into the Players table
$statement = $conn->prepare("
INSERT INTO `Players`
(PlayerID, Score1, Score2)
VALUES
(?,0,0)
");
$statement->bind_param("i", $playerID);
$statement->execute();
// Fetch all the players' data and output interface
$result = $conn->query("SELECT * FROM `Players`");
while ($row = $result->fetch_assoc()){
printf("ID %d | Score1: %d Score2 : %d", $row['PlayerID'], $row['Score1'], $row['Score2']);
}
print("Finished queries successfully.");
}else{
print("No player ID was received.");
}
}
And see what output return you get from :PostAsync on the Roblox side
One last thing, If I want to get information from the DataBank to Roblox,
On Roblox I probably need to use the HttpService:RequestAsync , But how dont see how it work.
Can you give me an example ?