Skip to content
This repository has been archived by the owner on May 13, 2022. It is now read-only.

Examples

Reece Harris edited this page Jul 8, 2021 · 10 revisions

Adding Player to database on join

local DataStore3 = require(game:GetService("ServerScriptService").DataStore3Libary);

-- Database hostname eg. (IPV4, URL) [DO NOT INCLUDE HTTP:// OR WWW.]
local url = ""
-- Key used to access database
local apiKey = ""
-- Database owner username (not roblox username)
local username = ""


local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)  -- Player Join function
	local userid = player.UserId  -- Get UserId of joined player
	local payload = "SELECT * FROM userData WHERE userId ='"..userid.."'"  -- Select all from userdata table where userId = players userid (SQL CODE)
	local response = DataStore3.GetPayload(url, apiKey, username, payload)  -- Send the post request to the server

	if response.Response == nil then  -- If the responce is nil (Meaning the player isnt already in the data base)
		local payload = "INSERT INTO userData VALUES ('"..userid.."', '0', '0', '0')"  -- Add the player to the database making the first value the userId (SQL CODE)
		local response = DataStore3.PostPayload(url, apiKey, username, payload)  -- Send the post request to the server
	else  -- If the user is in the databse
		local payload = "SELECT * FROM userData WHERE userId = '"..player.UserId.."'"  -- Select all data from the userdata table where userId = players userid (SQL CODE) 
		local response = DataStore3.GetPayload(url, apiKey, username, payload)  -- Send the get request to the server
		
		-- When getting a responce there are to parts the the table 'response.Response[a][b]' A & B a is the selector for example if a was equal to 1 you would 
		-- get the response code (1 is success and 0 is error) if a was equal to 2 you would get the SQL response for this example (userId, Gold, Wood, Gems)

		player.leaderstats.Gold.Value = response.Response[1][2]  -- Make the leaderstat of the player equal to the usersData (Gold, 2nd column in row)
		player.leaderstats.Gems.Value = response.Response[1][4]  -- Make the leaderstat of the player equal to the usersData (Gems, 4nd column in row)
		player.Inventory.Wood.Value = response.Response[1][3]  -- Make the leaderstat of the player equal to the usersData (Wood, 3nd column in row)
	end
end)

Saving Player on leave

local DataStore3 = require(game:GetService("ServerScriptService").DataStore3Libary);



-- Database hostname eg. (IPV4, URL) [DO NOT INCLUDE HTTP:// OR WWW.]
local url = ""

-- Key used to access database
local apiKey = ""

-- Database owner username (not roblox username)
local username = ""

local Players = game:GetService("Players")


Players.PlayerRemoving:Connect(function(player)
	local gold = player.leaderstats.Gold.Value
	local gems = player.leaderstats.Gems.Value
	local wood = player.Inventory.Wood.Value
	local payload = "UPDATE userData SET gold ='"..gold.."', wood ='"..wood.."', gems='"..gems.."' WHERE userId = '"..player.UserId.."';"
	local response = DataStore3.PostPayload(url, apiKey, username, payload)
end)

Saving Player data every 30 seconds

local DataStore3 = require(game:GetService("ServerScriptService").DataStore3Libary);



-- Database hostname eg. (IPV4, URL) [DO NOT INCLUDE HTTP:// OR WWW.]
local url = ""

-- Key used to access database
local apiKey = ""

-- Database owner username (not roblox username)
local username = ""

local Players = game:GetService("Players")

while true do
	for i,v in pairs(game.Players:GetChildren()) do
		local gold = v.leaderstats.Gold.Value
		local gems = v.leaderstats.Gems.Value
		local wood = v.Inventory.Wood.Value
		local payload = "UPDATE userData SET gold ='"..gold.."', wood ='"..wood.."', gems='"..gems.."' WHERE userId = '"..v.UserId.."';"
		local response = DataStore3.PostPayload(url, apiKey, username, payload)
	end
	wait(30)
end
Clone this wiki locally