Here is a Script and a ModuleScript. There are two reason that I am using a ModuleScript: 1st it allows you to have shared code between several scripts (code once use many times), 2nd I had already written it.
So here it is:
Structure of game.ServerScriptService
Module script in game.ServerScriptService
local DataStoreService = game:GetService('DataStoreService')
T = {DataStoreRetries = 5}
function T:GetData(Store, Key)
Data = nil
if Store and Key then
local tries = 0
repeat
tries = tries + 1
success, message = pcall(function()
DataStore = DataStoreService:GetDataStore(Store)
Data = DataStore:GetAsync(Key)
end)
if not success then
wait(1)
end
until tries == T.DataStoreRetries or success
if not success then
print("")
print("require(_G.M.DS):GetData, Error Data not loaded from DataStore")
print("Current values: Store = "..Store..", Key = "..Key)
error(message)
end
end
return Data
end
function T:SetData(Store, Key, Value)
if Store and Key and Value then
local tries = 0
repeat
tries = tries + 1
success, message = pcall(function()
DataStore = DataStoreService:GetDataStore(Store)
if not T:GetData(Key) then
DataStore:SetAsync(Key, Value)
end
end)
if not success then
wait(1)
end
until tries == T.DataStoreRetries or success
if not success then
print("")
print("require(_G.M.DS):SetData, Error Data not saved to DataStore")
print("Current values: Store = "..Store..", Key = "..Key..", Value = "..Value)
error(message)
end
end
end
function T:ForceSetData(Store, Key, Value)
if Store and Key and Value then
local tries = 0
repeat
tries = tries + 1
success, message = pcall(function()
DataStore = DataStoreService:GetDataStore(Store)
DataStore:SetAsync(Key, Value)
end)
if not success then
wait(1)
end
until tries == T.DataStoreRetries or success
if not success then
print("")
print("require(_G.M.DS):ForceSetData, Error Data not saved to DataStore")
print("Current values: Store = "..Store..", Key = "..Key..", Value = "..Value)
error(message)
end
end
end
function T:UpdateData(Store, Key, Value)
if Store and Key and Value then
local tries = 0
repeat
tries = tries + 1
success, message = pcall(function()
DataStore = DataStoreService:GetDataStore(Store)
DataStore:UpdateAsync(Key, function(OldValue)
OldValue = OldValue or 0
local NewValue = OldValue
if type(Value) == "number" then
NewValue = NewValue + Value
end
return NewValue
end)
end)
if not success then
wait(1)
end
until tries == T.DataStoreRetries or success
if not success then
print("")
print("require(_G.M.DS):UpdateData, Error Data not saved to DataStore")
print("Current values: Store = "..Store..", Key = "..Key..", Value = "..Value)
error(message)
end
end
end
return T
Script in game.ServerScriptService
local DataStore = game:GetService("DataStoreService")
local ds1 = DataStore:GetDataStore("Currency")
ID = "player"
game.Players.PlayerAdded:connect(function(player)
local stats = Instance.new("Folder",player)
stats.Name = "leaderstats"
local Schmeckles = Instance.new("IntValue",stats)
Schmeckles.Name = "Schmeckles"
local key = ID..player.userId
Schmeckles.Value = require(game.ServerScriptService.DS):GetData("Currency", key) or 0
end)
game.Players.PlayerRemoving:connect(function(player)
local key = ID..player.userId
require(game.ServerScriptService.DS):ForceSetData("Currency", key, player.leaderstats.Schmeckles.Value)
end)
while wait(60) do
P = game:GetService("Players"):GetChildren()
for i=1, #P do
if P[i]:FindFirstChild("leaderstats") then
if P[i].leaderstats:FindFirstChild("Schmeckles") then
local key = ID..P[i].userId
require(game.ServerScriptService.DS):ForceSetData("Currency", key, P[i].leaderstats.Schmeckles.Value)
end
end
end
end
game.OnClose = function()
wait(10)
end
2 comments
Remove lines 13 - 17, as well as 26 - 28. There is no need for them. Everything else looks fine. Make sure this script is in Workspace or ServerScriptStorage.
They're used to update the value whenever it's changed and to allow the datastore to update before the game closes respectively. This script is in ServerScriptStorage with API turned on. I still don't see why the script isn't working