So past 24 hours, I am trying somehow to save data in MySQL Database from Roblox I’ve managed to save a string, boolean, number etc but when I tried saving JSON it gave me an error on my website saying
SyntaxError: Unexpected token S in JSON at position 0
at JSON.parse (<anonymous>)
at IncomingMessage.<anonymous> (C:\Users\Luka\Desktop\Lua\index.js:27:21)
at IncomingMessage.emit (events.js:203:15)
at endReadableNT (_stream_readable.js:1129:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
PS C:\Users\Luka\Desktop\Lua>
Ohh and also I forgot to say I am sending an HTTP request with some details from Roblox to my local website and then that local website puts the information in the database
This is the current code I have
local MySQL = require(script.MySQL).new("http://localhost:8000/")
local Players = game:GetService("Players")
local DataLoaded = {}
Players.PlayerAdded:Connect(function(Player)
local ls =Instance.new("Folder",Player)
ls.Name = "leaderstats"
Instance.new("NumberValue",ls).Name = "Money"
local Success,Res = MySQL:SELECT([[id]],Player.UserId)
if (Success and Res.Body) then
local Success,Res = MySQL:JSONDecode(Res.Body)
if (Success and Res[1]) then
DataLoaded[Player.UserId] = true
for i,v in next,Res[1] do
if (type(v) == "table") then
for _,e in next,v do
ls[e.Name].Value = e.Value
end
end
end
end
end
end)
function Data(folder)
local DataTable = {}
for _,v in next,folder:GetChildren() do
local TempData = {
Name = v.Name,
Value = v.Value
}
table.insert(DataTable,TempData)
end
return DataTable
end
Players.PlayerRemoving:Connect(function(Player)
if (DataLoaded[Player.UserId]) then
local Set = [[name="%s",id=%s,data=%s]]
local Success,Res = MySQL:JSONEncode(Data(Player.leaderstats))
if not (Success) then
print(Res)
return
end
Set = string.format(Set,Player.Name,Player.UserId,Res)
local WHERE = [[name="%s"]]
WHERE = string.format(WHERE,Player.Name)
MySQL:UPDATE(Set,WHERE)
else
local Success,Res = MySQL:JSONEncode(Data(Player.leaderstats))
if not (Success) then
print(Res)
return
end
local String = [["%s","%s","$s"]]
String = string.format(String,Player.Name,Player.UserId,Res)
local Success,Res = MySQL:INSERT([[`name`, `id`, `data`]],String)
if not (Success) then
print(Res)
end
end
end)
Website
var mysql = require('mysql');
var express = require("express")
var app = express()
var con = mysql.createConnection({
host: "",
user: "",
password: "",
database: ""
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
app.post("/",function(req,res) {
var body = '';
req.on('data', function (data) {
body += data;
});
req.on('end', function () {
body = JSON.parse(body)
console.log(body.Q)
con.query(body.Q, function (err, result) {
if (err) throw err;
res.end(JSON.stringify(result))
});
});
})
app.listen(8000)