What I want?
I want to find the user from google sheets, and from there user I want to get the amount
of credits that they have. I also want to be able to change (post) the amount of credits.
Current Code
local ScriptID = ""-- script id was here i removed
local URL = "https://script.google.com/macros/s/" .. ScriptID .. "/exec"
local httpService = game:GetService("HttpService")
local module = {}
game:WaitForChild("NetworkServer")
function decodeJson(JSON)
local jsonTab = {}
pcall(function()
jsonTab = httpService:JSONDecode(JSON)
end)
return jsonTab
end
function encodeJson(data)
local jsonString = data
pcall(function()
jsonString = httpService:JSONEncode(data)
end)
return jsonString
end
function doGet(GoogleSheet, KeyID)
local JSON = httpService:GetAsync(URL .. "?sheet=" .. GoogleSheet .. "&user=" .. KeyID)
local data = decodeJson(JSON)
if data.result == "success" then
return data.value
else
return
end
end
function doPost(GoogleSheet, KeyID, data)
local JSON = httpService:UrlEncode(encodeJson(data))
local RTJSON = httpService:PostAsync(URL, "sheet=" .. GoogleSheet .. "&user=" .. KeyID .. "&robux=" .. JSON, 2)
local data = decodeJson(RTJSON)
if data.result == "success" then
return true
else
return false
end
end
function module:GetDatabase(GoogleSheet)
local database = {}
function database:PostAsync(KeyID, value)
return doPost(GoogleSheet, KeyID, value)
end
function database:GetAsync(KeyID)
return doGet(GoogleSheet, KeyID)
end
return database
end
return module
Script
databaseService = require(script.DatabaseService)
local globalDatabase = databaseService:GetDatabase("Global")
game.Players.PlayerAdded:Connect(function(player)
print(globalDatabase:GetAsync(player.UserId))
end)
I am very confused on how to do this, if you can help that would be awesome!