This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Examples
Reece Harris edited this page Jul 9, 2021
·
10 revisions
CREATE TABLE userData( -- Create a table called 'userData'
userId VARCHAR NOT NULL, -- Make the first column in the table called 'userId' as a data type 'VARCHAR' and make it 'NOT NULL'
gold INT(10), -- Make the second column in the table called 'gold' as data type 'INT' however only allow 10 digit numbers eg. 1000000000
wood INT(10), -- Make the second column in the table called 'wood' as data type 'INT' however only allow 10 digit numbers eg. 1000000000
gems INT(10), -- Make the second column in the table called 'gems' as data type 'INT' however only allow 10 digit numbers eg. 1000000000
PRIMARY KEY(userId) -- Make a primary key and assign it to 'userId'
)
local DataStore3 = require(game:GetService("ServerScriptService").DataStore3Libary);
local payload = [[
CREATE TABLE userData(
userId VARCHAR NOT NULL,
gold INT(10),
wood INT(10),
gems INT(10),
PRIMARY KEY(userId)
)
]]
local response = DataStore3.GetPayload(payload)
local DataStore3 = require(game:GetService("ServerScriptService").DataStore3Libary);
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(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(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(payload) -- Send the get request to the server
-- When getting a response there are two parts 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 an 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)
local DataStore3 = require(game:GetService("ServerScriptService").DataStore3Libary);
local Players = game:GetService("Players")
Players.PlayerRemoving:Connect(function(player) -- Player Leave function
gold = player.leaderstats.Gold.Value -- Get players leaderstat values (Gold)
gems = player.leaderstats.Gems.Value -- Get players leaderstat values (Gold)
wood = player.Inventory.Wood.Value -- Get players leaderstat values (Gold)
local payload = "UPDATE userData SET gold ='"..gold.."', wood ='"..wood.."', gems='"..gems.."' WHERE userId = '"..player.UserId.."';" -- Update userData table and set Gold, Wood, Gems where usersId = Players userId
local response = DataStore3.PostPayload(payload) -- Send the Post request to the server
end)
local DataStore3 = require(game:GetService("ServerScriptService").DataStore3Libary);
local Players = game:GetService("Players")
while true do -- Infinate Loop
for i,v in pairs(game.Players:GetChildren()) do -- Get all active players in game
local gold = v.leaderstats.Gold.Value -- Get players Gold Value
local gems = v.leaderstats.Gems.Value -- Get players Gems Value
local wood = v.Inventory.Wood.Value -- Get players Wood Value
local payload = "UPDATE userData SET gold ='"..gold.."', wood ='"..wood.."', gems='"..gems.."' WHERE userId = '"..v.UserId.."';" -- Update table called userData and set gold, wood and gems where usersId = players user id
local response = DataStore3.PostPayload(payload) -- Send the Post request to the server
end
print("Saved Successfully") -- Print to server 'Saved Successfully' only developers will see this as players cant access the server console
wait(30) -- Wait 30 seconds to loop again
end