I need help with my DataStore

Hello,

First sorry for my bad english.

I have a problem with my DataStore.
Script print Data saved successfully but it isn’t.

I have script located in ServerScriptService.
Here is my script:

--SETTINGS--
local DataStoreName = "KnifeBattleDataStore"


--VARIABLES--
local DataStoreService = game:GetService("DataStoreService")
local MyDataStore = DataStoreService:GetDataStore(DataStoreName)


--FUNCTIONS--

   --PLAYER ADDED--
       game.Players.PlayerAdded:Connect(function(Player)
	        -----------NUMBERS FOLDER------------
            local Numbers = Instance.new("Folder")
            Numbers.Name = "Numbers"
            Numbers.Parent = Player
            
            -----------MONEY VALUE---------------
            local Money = Instance.new("IntValue")
            Money.Name = "Money"
            Money.Parent = Numbers

            -----------GEMS VALUE---------------
            local Gems = Instance.new("IntValue")
            Gems.Name = "Gems"
            Gems.Parent = Numbers

            -----------LEVEL VALUE---------------
            local Level = Instance.new("IntValue")
            Level.Name = "Level"
            Level.Parent = Numbers

            -----------EXPERIENCE VALUE---------------
            local Experience = Instance.new("IntValue")
            Experience.Name = "Experience"
            Experience.Parent = Numbers

   --DATA LOAD--
            local MoneyData   
            local GemsData
            local LevelData
            local ExperienceData
  
            local success, errormessage = pcall(function()
	        MoneyData = MyDataStore:GetAsync(Player.UserId.."-Money")
	        GemsData = MyDataStore:GetAsync(Player.UserId.."-Gems")
	        LevelData = MyDataStore:GetAsync(Player.UserId.."-Level")
	        ExperienceData = MyDataStore:GetAsync(Player.UserId.."-Experience")
       end)
       
       if success then
	        Money.Value = MoneyData
	        Gems.Value = GemsData
	        Level.Value = LevelData
	        Experience.Value = ExperienceData
       else
	        print("There was an error whilist getting your data.")
	        warn(errormessage)
       end         	
       end)
   
   --PLAYER REMOVING--
       game.Players.PlayerRemoving:Connect(function(Player)
	
   --DATA SAVE--
            local success, errormessage = pcall(function()
	        MyDataStore:SetAsync(Player.UserId.."-Money",Player.Numbers.Money.Value)
	        MyDataStore:SetAsync(Player.UserId.."-Gems",Player.Numbers.Gems.Value)
	        MyDataStore:SetAsync(Player.UserId.."-Level",Player.Numbers.Level.Value)
	        MyDataStore:SetAsync(Player.UserId.."-Experience",Player.Numbers.Experience.Value)
       end)

       if success then
	        print("Player Data successfully saved!")
       else
	        print("There was an error when saving data.")
	        warn(errormessage)
       end
       end)
1 Like

well I can notice a couple of issues

you’re using seperate keys for the same datastore, you should just uses tables it’s more efficient and easier

you’re also setting the getasync data even if their is no data to get (e.g new player) so you would want to use or

fixed code below
(there might be more issues but none I can see right now)

--SETTINGS--
local DataStoreName = "KnifeBattleDataStore"


--VARIABLES--
local DataStoreService = game:GetService("DataStoreService")
local MyDataStore = DataStoreService:GetDataStore(DataStoreName)


--FUNCTIONS--

   --PLAYER ADDED--
       game.Players.PlayerAdded:Connect(function(Player)
	        -----------NUMBERS FOLDER------------
            local Numbers = Instance.new("Folder")
            Numbers.Name = "Numbers"
            Numbers.Parent = Player
            
            -----------MONEY VALUE---------------
            local Money = Instance.new("IntValue")
            Money.Name = "Money"
            Money.Parent = Numbers

            -----------GEMS VALUE---------------
            local Gems = Instance.new("IntValue")
            Gems.Name = "Gems"
            Gems.Parent = Numbers

            -----------LEVEL VALUE---------------
            local Level = Instance.new("IntValue")
            Level.Name = "Level"
            Level.Parent = Numbers

            -----------EXPERIENCE VALUE---------------
            local Experience = Instance.new("IntValue")
            Experience.Name = "Experience"
            Experience.Parent = Numbers

   --DATA LOAD--
            local Data

            local success, errormessage = pcall(function()
                    Data = MyDataStore:GetAsync(Player.UserId) or {Money = 0, Gems = 0, Level = 0, Experience = 0} -- get the data and if data is nil set it to a preset table
            end)
       
       if success then
	        Money.Value = Data.Money
	        Gems.Value = Data.Gems
	        Level.Value = Data.Level
	        Experience.Value = Data.Experience
       else
	        print("There was an error whilist getting your data.")
	        warn(errormessage)
       end         	
       end)
   
   --PLAYER REMOVING--
       game.Players.PlayerRemoving:Connect(function(Player)
	
   --DATA SAVE--
            local Data = {Player.Numbers.Money.Value, Player.Numbers.Gems.Value, Player.Numbers.Level.Value, Player.Numbers.Experience.Value} -- the users data
            local success, errormessage = pcall(function()
	        MyDataStore:SetAsync(Player.UserId, Data) -- save the data
            end)

       if success then
	        print("Player Data successfully saved!")
       else
	        print("There was an error when saving data.")
	        warn(errormessage)
       end
       end)
2 Likes

Well really it’s only mentions of improvement in the code over anything wrong.

  • Not using GetService to get the Players service
  • Using those accursed ValueObjects (as defined by a lot of developers)
  • Not using pcall effectively [1]
  • No error handling if any data values are blank

[1]:

-- Wrap the method, not an anonymous function
local success, data = pcall(MyDataStore.GetAsync, MyDataStore, Player.UserId)

Do NOT return a default table if GetAsync returns nil, handle missing or corrupted data elsewhere. Data loss may be easier to reach at this point, including if a load fails (since there’s no proper error handling here).

1 Like

Apologies was just focusing on fixing his datastore related issues, but I guess I was a bit wrong on that, I just used what I saw on the wiki