The script I use is from this https://developer.roblox.com/en-us/articles/Saving-Player-Data with some added stuff. Here’s the entire script though the setdata and getdata functions are what I added.
--[Services]--
local DataStoreService = game:GetService("DataStoreService")
local runService = game:GetService("RunService")
--[Data Stores]--
local dataStore
if runService:IsStudio() then
print("USING TESTING DATASTORE")
dataStore = DataStoreService:GetDataStore("TestingV100")
else
print("USING LIVE DATASTORE")
dataStore = DataStoreService:GetDataStore("LiveV10")
end
--[Remote Events / Functions]--
local dataEvent = game.ReplicatedStorage.Events:WaitForChild("data")
--[Variables]--
local HasPlayed = false
local PlayerStatManager = {}
local sessionData = {} --Table to hold player information for the current session
local autosaveTime = 60
-- Function that other scripts can call to change a player's stats
function PlayerStatManager:ChangeStat(player, statName, value)
local playerUserId = "Player_" .. player.UserId
assert(typeof(sessionData[playerUserId][statName]) == typeof(value), "ChangeStat error: types do not match")
if typeof(sessionData[playerUserId][statName]) == "number" then
sessionData[playerUserId][statName] = sessionData[playerUserId][statName] + value
dataEvent:FireClient(player,PlayerStatManager:GetData(player))
elseif typeof(sessionData[playerUserId][statName]) == "table" then
sessionData[playerUserId][statName] = value
else
sessionData[playerUserId][statName] = value
end
end
function PlayerStatManager:GetData(player, statName)
local playerUserId = "Player_" .. player.UserId
assert(sessionData[playerUserId], "GetData error: Data for " .. player.Name .. " does not exist.") --if data does not exist for player, then error
if statName then
local value = sessionData[playerUserId][statName]
assert(value, "GetData error: Value does not exist for: ".. statName) --if there is no data for specified stat, then error
return value -- if we specified a specific stat, then return the data for that stat
else
return sessionData[playerUserId] --if we didn't specify a specify stat, then return all the player's data
end
end
function PlayerStatManager:setData(playerUserId)
print("Saving")
if sessionData[playerUserId] then
local tries = 0
local success
repeat
tries = tries + 1
success = pcall(function()
dataStore:SetAsync(playerUserId, sessionData[playerUserId])
end)
if not success then wait(1) end
until tries == 3 or success
if not success then
warn("Cannot save data for player!")
end
end
end
-- Function to add player to the "sessionData" table
local function setupPlayerData(player)
local playerUserId = "Player_" .. player.UserId
local success, data = pcall(function()
return dataStore:GetAsync(playerUserId)
end)
if success then
if data then
-- Data exists for this player
HasPlayed = true
print(player.Name.." has data")
sessionData[playerUserId] = data
else
-- Data store is working, but no current data for this player
print(player.Name.." doesn't have data")
sessionData[playerUserId] = {
newPlayer = "true", --will be used for inititating the tutorial if the player plays the tutorial this will beupdated to false
isBanned = "false",
banReason = "",
dungeonLevel = 1,
level = 1,
xp = 0,
gold = 99999,
caps = 0,
rep = 0,
dimension = 0,
power = 0,
stamina = 0,
agility = 0,
inventory = {}, --items, materials, potions etc will be stored here
equipped = {
mainhand = "",
offhand = "",
head = "",
body = "",
neck = "",
ring = "",
accessory = "",
pet = "",
},
Owned = {
--Upgrades
--Sodas
["Peasent Pop"] = 1,
--Customizations
["Dirt"] = 1,
["Busted"] = 1,
},
currentTavernCustomizations = { --current customizations (used when loading the players tavern
currentFloorMaterial = "Grass",
currentFloorColor = {R = 86,G = 66,B = 54},
currentCounterMaterial = "CorrodedMetal",
currentCounterColor = {R = 128, G = 73, B = 27},
}
}
end
else
warn("Cannot access data store for player!")
end
end
-- Function to save player's data
local function savePlayerData(playerUserId)
if sessionData[playerUserId] then
local tries = 0
local success
repeat
tries = tries + 1
success = pcall(function()
dataStore:SetAsync(playerUserId, sessionData[playerUserId])
end)
if not success then wait(1) end
until tries == 3 or success
if not success then
warn("Cannot save data for player!")
end
end
end
-- Function to save player data on exit
local function saveOnExit(player)
print("Saving")
local playerUserId = "Player_" .. player.UserId
savePlayerData(playerUserId)
end
-- Function to periodically save player data
local function autoSave()
while wait(autosaveTime) do
print("Auto Saving")
for playerUserId, data in pairs(sessionData) do
savePlayerData(playerUserId)
end
end
end
-- Start running "autoSave()" function in the background
spawn(autoSave)
-- Connect "setupPlayerData()" function to "PlayerAdded" event
game.Players.PlayerAdded:Connect(setupPlayerData)
-- Connect "saveOnExit()" function to "PlayerRemoving" event
game.Players.PlayerRemoving:Connect(saveOnExit)
return PlayerStatManager