Datastore not working?

So im new to datastore, and the one i used before is very unreliable, so im trying to redo it, and I cannot figure out how, any help?

game.Players.PlayerAdded:Connect(function(player)
	local DataStore = game:GetService("DataStoreService"):GetDataStore('taxzcxa')
	
	local currentsave
	local stats
	local currentstat
	local tabletosave

	currentsave = DataStore:GetAsync(player.UserId)
	stats = player:WaitForChild("stats")
	
	local cash = player:FindFirstChild("stats"):FindFirstChild("Cash")
	cash.Value = currentsave[cash]['Value']
	
	local eye = player:FindFirstChild("stats"):FindFirstChild("EyeColor")
	eye.Value = currentsave[eye]['Value']

	end)

What is the datastore you used before this?

this

> local DoAutoSave = true
> local AutoSaveInterval = 120
> 
> local DataStore = game:GetService("DataStoreService"):GetDataStore('taxzcxa')
> 
> local currentsave
> local stats
> local currentstat
> local tabletosave
> 
> game.Players.PlayerAdded:Connect(function(player)
> 	repeat wait() until player:FindFirstChild("stats")
> 	wait()
> 	currentsave = DataStore:GetAsync(player.UserId)
> 	stats = player:WaitForChild("stats")
> 	if currentsave then
> 		for i = 1,#currentsave do
> 			currentstat = stats:WaitForChild(currentsave[i]['Name'])
> 			currentstat.Value = currentsave[i]['Value']
> 		end
> 		print('Loaded stats')
> 	else
> 		print('Player is new and yes do be')
> 	end
> 	if DoAutoSave then
> 		spawn(function()
> 			repeat
> 				wait(AutoSaveInterval)
> 				if player then
> 					tabletosave = {}
> 					for _,v in pairs(player:WaitForChild("stats"):GetChildren()) do
> 						tabletosave[#tabletosave+1] = {Name = v.Name,Value = v.Value}
> 					end
> 					DataStore:SetAsync(player.UserId,tabletosave)
> 				else
> 					print('Player is not there, he dipped"')
> 				end
> 			until not player
> 		end)
> 	end
> end)
> 
> game.Players.PlayerRemoving:Connect(function(player)
> 	if player then
> 		tabletosave = {}
> 		for _,v in pairs(player:FindFirstChild("stats"):GetChildren()) do
> 			tabletosave[#tabletosave+1] = {Name = v.Name,Value = v.Value}
> 		end
> 		DataStore:SetAsync(player.UserId,tabletosave)
> 	else
> 		print("The man is not there")
> 	end
> end)
> 
> game.ReplicatedStorage.Save.OnServerEvent:Connect(function(player)
> 	if player then
> 		tabletosave = {}
> 		for _,v in pairs(player:WaitForChild("stats"):GetChildren()) do
> 			tabletosave[#tabletosave+1] = {Name = v.Name,Value = v.Value}
> 		end
> 		DataStore:SetAsync(player.UserId,tabletosave)
> 		print("did save")
> 	else
> 		print("The man is not there")
> 	end
> end)

What exactly is stats? A table?

its a folder inside the player with values inside of it

Okay so it is probably leaderstats.

its called stats, the script works, but its unreliable, sometimes giving other players other players stats, or sometimes just wiping them completely.

1 Like

Okay what are the values in stats and what kind of values are they? Also what do you want to save and when do you want to save it?

Its just character stats, like hair, cash, etc. I would just like it to autosave maybe every 3 minutes, and to save when they leave aswell.

1 Like

Try this code:

local dss = game:GetService("DataStoreService")
local DataStore = dss:GetDataStore("taxzcxa")

game.Players.PlayerAdded:Connect(function(player)
        local playerUserId = "Player_"..player.UserId
        local stats = player:WaitForChild("stats")
        local cash = stats.Cash
        local eye = stats.EyeColor

        local currentsave
        local success, errormessage = pcall(function()
               currentsave = DataStore:GetAsync(playerUserId)
        end)
        
        if currentsave then
            if success then
                 cash.Value = currentsave.Cash
                 eye.Value = currentsave.Eye
              end
        end
        
        local function saveData()
               while wait(180) do
                       local currentsave = {
                            Cash = stats.Cash.Value
                            Eye = stats.EyeColor.Value
                       }

                       local s, m = pcall(function()
                           DataStore:SetAsync(currentsave)
                       end)

                       if s then
                              print("Successfully saved data")
                       else
                              print("There was an error while saving data")
                              warn(errormessage)
                      end
               end
        end

        spawn(saveData)
end)

game.Players.PlayerRemoving:Connect(function(player)
        local playerUserId = "Player_"..player.UserId
        local stats = player:WaitForChild("stats")
        local cash = stats.Cash
        local eye = stats.EyeColor         
        
        local currentsave = {
          Cash = stats.Cash.Value
          Eye = stats.EyeColor.Value
         }

          local success, errormessage = pcall(function()
               DataStore:SetAsync(currentsave)
        end)

        if success then
              print("Successfully saved data")
        else
               print("There was an error while saving data")
               warn(errormessage)
       end
end)

It might have some errors though I did not test it because I am in a car and my battery is low so I want to send it before my laptop dies I can fix errors later. Tell me the errors if you have some.
I can explain the code later if you want.

sadly, it doesn’t seem to be working, no errors though.

Try playing the actual game. Sometimes the datastore does not work in studio for some reason. You can use the console to help change the leaderstats. The hotkey is f9 or you can type “/console”

Are you getting any errors in your console?

Assuming you are testing this in studio. Ensure you go to Game Settings > Security and “Enable Studio Access to API Services” is on.

image

This will allow studio to use DataStores. If you have already done this or enabling it doesn’t fix your issue, let me know.

Oops I forgot about saying to do that.