Am I doing DataStore wrong?

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

Problem: Doesn’t print ‘found table’

3 Likes

What’s exactly the issue? Are there any errors in output? Is data saving, not loading, etc.?

There doesn’t appear to be any errors i’m seeing in the output. It’s showing up blank, I could try adding prints to see where it isn’t working.

What’s the issue though? Data isn’t saving or isn’t loading?

Are you actually calling the functions that are in the module? If not, these are functions and will not run on their own unless they are called.

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.

I see the problem. You have SetAsync backwards.
You’re doing SetAsync(DataTable, Key)
It should be SetAsync(Key, DataTable)

2 Likes

Okay yeah that fixed the issue, now i’m given an error saying that this part returns a nil value;

if playerData then
				
	steps.Value = playerData.dataTable.steps
	hoops.Value = playerData.dataTable.hoops
	wins.Value = playerData.dataTable.wins
	rebirth.Value = playerData.dataTable.rebirths

end

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.

Apparently dataTable isn’t even saving in the script no clue why

Make sure when writing a post to explain what’s going wrong because I have no clue where to start looking for the problem

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.

Also when the player leaves the game that’s when i run the saving part of the module.

Are you testing this in the studio or in the game?

The actual game, I’ve tried test before doesn’t save in test servers.
(which it’s not supposed to save in test)

Ohh, I found the error there is nothing called datatable in your DataStore when you load it.

You are saving

			local dataTable = {
				steps = steps.Value;
				hoops = hoops.Value;
				wins = wins.Value;
				rebirths = rebirth.Value;
			}

and not

			local dataTable = {
                dataTable = {
				 steps = steps.Value;
				 hoops = hoops.Value;
				 wins = wins.Value;
				 rebirths = rebirth.Value;
               }
			}

You need to remove if playerData.dataTable then

It should look something like this

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 table'
			steps.Value = playerData.steps
			hoops.Value = playerData.hoops
			wins.Value = playerData.wins
			rebirth.Value = playerData.rebirths
	elseif playerData == nil then
				
		--[[print'no data'
				
		local dataTable = {
			steps = 1;
			hoops = 1;
			wins = 1;
			rebirths = 1;
		}
				
		data:SetAsync(key,dataTable)]]
				
	end
	
end
2 Likes

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.