So I tried making a module for when the player joins it gives them their saved stats, and when they leave it saves. What am I doing wrong? How to fix can someone help?
local module = {}
local data = game:GetService("DataStoreService"):GetDataStore("SpeedsterData")
function loadStats(player, stats)
local steps = stats:FindFirstChild("Steps")
local hoops = stats:FindFirstChild("Hoops")
local wins = stats:FindFirstChild("Wins")
local rebirth = stats:FindFirstChild("Rebirth")
local key = player.userId.."-key"
local playerData = data:GetAsync(key)
if playerData then
print'found data'
steps.Value = playerData.steps
wins.Value = playerData.wins
rebirth.Value = playerData.rebirths
hoops.Value = playerData.hoops
end
end
module.addData = function(player)
spawn(function()
local stats = player:WaitForChild("leaderstats")
print'loading stats'
loadStats(player,stats)
end)
end
module.saveData = function(player)
spawn(function()
local key = player.userId.."-key"
local playerData = data:GetAsync(key)
local stats = player:FindFirstChild("leaderstats")
if playerData and stats then
print'found data and stats'
local steps = stats:FindFirstChild("Steps")
local hoops = stats:FindFirstChild("Hoops")
local wins = stats:FindFirstChild("Wins")
local rebirth = stats:FindFirstChild("Rebirth")
playerData.steps = steps.Value
playerData.hoops = hoops.Value
playerData.wins = wins.Value
playerData.rebirths = rebirth.Value
data:SetAsync(key,playerData)
end
end)
end
return module
It’s not saving or loading for me. And yeah, I’m calling the module functions from another script. It’s just 1 line so I figured it wasn’t necessary to show that part.
You need to do data:SetAsync() in the saveData function instead of playerData:SetAsnyc(). You don’t need the players data for SetAsync. Only their key.
How do I access dataTable in the script, through playerData? I tried playerData.dataTable, but it’s giving me an error and saying it doesn’t exist. It says playerData exists, just not dataTable.
I’ll update the script the problem is that dataTable isn’t saving correctly or I can’t access it the right way. Check the first post to see the updated code.
That’s a downgrade though, I’m trying to make it save a table to the datastore. There has to be a way to save the table to the playerData so I can access it.
This is how I do it that saves every value in a folder
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Test")
local Failed = {}
local DataStoreTable = {}
function DataStoreTable:Save(UserId,Folder)
if (Failed[UserId]) then
return
end
local Key = UserId.."-Key"
local Data = {}
for _,v in next,Folder:GetChildren() do
local TempData = {
Name = v.Name,
Value = v.Value
}
table.insert(Data,TempData)
end
pcall(function()
DataStore:SetAsync(Key,Data)
end)
end
function DataStoreTable:Get(UserId,Folder)
local Key = UserId.."-Key"
Failed[UserId] = false
local Success,Res = pcall(function()
return DataStore:GetAsync(Key)
end)
if not (Success) then
Failed[UserId] = true
print(Res)
return
end
if (Success and Res) then
for i,v in next,Res do
Folder[v.Name].Value = v.Value
end
end
end
return DataStoreTable
I’m really just learning about datastore, so this is very confusing to me. I’m trying to save a table to the datastore, and access it that is all I want to do. My script should be somewhat efficient I just don’t know why it isn’t saving anything.