This script is supposed to create and save a subset of player stats that is supposed to be saved and loaded upon the player joining and leaving respectively.
When I increase/change one of the data points and leave and rejoin, the data does not load. This script is currently being tested on a private game, if that matters at all.
What is the issue with it? Output gave me nothing to work with.
local DataStores = game:GetService("DataStoreService")
local PlayersDataStore = DataStores:GetDataStore("Players")
local FindValues = {
"Rank","XP","Prestige","Heists";
--Add any new variable!
}
local TypeValues = {
Rank = "IntValue"; --Or int whatever is fine...
XP = "IntValue";
Prestige = "IntValue";
Heists = "IntValue";
}
function SetupPlayer(Player)
local LeaderStats = Instance.new("Folder",Player)
LeaderStats.Name = "perm"
local ValueObjects = {}
for i,v in pairs(FindValues)do
local VALUE = Instance.new(TypeValues[v] or "StringValue",LeaderStats)
VALUE.Name = v
ValueObjects[i] = VALUE
end
local LeaderTable = {}
local PlayerDS = PlayersDataStore:GetAsync(Player.UserId)
if not PlayerDS then --For new players!
local SetTable = {}
for i,v in pairs(ValueObjects)do
SetTable[FindValues[i]] = v.Value
end
PlayersDataStore:SetAsync(Player.UserId,SetTable)
PlayerDS = PlayersDataStore:GetAsync(Player.UserId)
end
for i,v in pairs(ValueObjects)do
v.Value = LeaderTable[FindValues[i]]
end
PlayersDataStore:SetAsync(Player.UserId,PlayerDS)
spawn(function()
while wait(10)do --Autosave
for i,v in pairs(ValueObjects)do
PlayerDS[FindValues[i]] = v.Value
end
PlayersDataStore:SetAsync(Player.UserId,PlayerDS)
end
end)
end
game.Players.PlayerAdded:connect(SetupPlayer)

6 comments
Don't forget to add a PlayerRemoving event so you can save players data when they leave the game.
Make sure studio access to API services is enabled as well
PlayerDataStore:SetAsync(Player.UserId, SavingTable) is undefined in the script?
They're in lines 24 42
Instead of SetAsync to update a player's data who already has data and isn't new, shouldn't you use UpdateAsync?
Yes but they both have the same function