DataStore problem

I keep watching youtube tutorials and I use them to learn how to import new additions to my game but every tutorial for datastore creates a new leaderstat, (New Cash or Money in the leaderboard). How do I make a script where it saves data from a pre-existing leaderstat?

1 Like

This script is FAR from perfect. I just wrote it up based on a data store I created a while back.
This would be a “Script” underneath ServerScriptService.
Line 3 the “StartTable” name the stuff in [“”] to whatever the values name is underneath leaderstats. Currently this save system only supports numberValues.
There are a whole bunch of stuff I know could be better, like 15 data store save requests all at the same time is worse then spreading it out. Nothing deletes the PlayerTable of a person when they leave the game either. I’m just to lazy to fix those because it’s not too big of a deal. Your players may experience some data loss, but hopefully it won’t be more then 30 seconds worth of it.

It is likely that this could error, I haven’t tested this out yet. Just post the output if it does and I’ll help you fix the problem.

local DataStore = game:GetService("DataStoreService")
local InfoStore = DataStore:GetDataStore("MySaveSystem")
local StartTable = { ["Money"] = 0, ["Level"] = 0}
local PlayerTables = {}

local settable = function(player)
     local f = player:WaitForChild("leaderstats")
     for key, value in pairs(PlayerTables[player]) do
         if f:FindFirstChild(key) then
              f:FindFirstChild(key).Value = value
         else
              print("There is no key named " .. key .. "underneath the leaderstats folder")
         end
     end
end

game.Players.PlayerAdded:Connect(function(plr)
	local UniqueKey = "YumDevelopersDataFor" .. plr.UserId
	local data = {}
    local success, CurrentStuff = pcall(function()
		return InfoStore:GetAsync(UniqueKey)
	end)	
    if success then
	    if CurrentStuff == nil then
		    data = StartTable
	     	InfoStore:SetAsync(UniqueKey, data)
	     else
		     data = CurrentStuff
	     end
        for key, value in pairs(StartTable) do
		    local There = false
		     for key2, value2 in pairs(data) do
			     if key == key2 then
				     There = true
			    end
		     end
		     if There == false then
			     data[key] = 0
		     end
	     end
         PlayerTables[plr] = data
	     settable(plr)
     else
        print("Error loading")
    end
end)
 
game.Players.PlayerRemoving:Connect(function(plr)
     local f = plr:FindFirstChild("leaderstats")
     for key, value in pairs(PlayerTables[plr]) do
         if f:FindFirstChild(key) then
              PlayerTables[plr][key] = f:FindFirstChild(key).Value 
         else
              print("There is no key named " .. key .. "underneath the leaderstats folder")
         end
     end
     InfoStore:SetAsync("YumDevelopersDataFor" .. plr.UserId, PlayerTables[plr])
end) 

while true do 
    wait(30)
    local players = game.Players:GetPlayers()
    for i = 1, #players do
        local plr = players[i]
        local f = plr:FindFirstChild("leaderstats")
         for key, value in pairs(PlayerTables[plr]) do
             if f:FindFirstChild(key) then
                  PlayerTables[plr][key] = f:FindFirstChild(key).Value 
             else
                  print("There is no key named " .. key .. "underneath the leaderstats folder")
             end
         end
         InfoStore:SetAsync("YumDevelopersDataFor" .. players[i].UserId, PlayerTables[players[i]])
    end
end

Edit: It’s getting fairly late for me It may take me till the morning to reply.

2 Likes

I’ve inserted the script into ServerScriptService. The value/leaderstat is already Money so I left it as that - this is the error that came up. (Top one)

1 Like

Sorry yea, I missed an end). I edited my previous post.

Oh okay, thanks, I’ll try it now.

(not all are for your script)

I guess this is what I get for not testing it myself.

I just refrenced player incorrectly I fixed my previous post, if you want to you can just go to line 8 and change PlayerTables[plr] to PlayerTables[player]

1 Like

Okay, if there’s any problems after you don’t have to fix them straight away if it’s late for you by the way - it’s late for me too so I probably won’t mess with it much after this one until tomorrow. Thanks.

That’s currently the error:


I apologize for the amount of replies/work you’re doing in order to help me out.

Thanks so much! The Money is now saving in-game, my bank and shop robbing system is now functioning and automatically saving. Thanks!

1 Like