DataStore2 Saving/Loading Not Working

So I made this beautiful code that doesn’t work. Basically, your data isn’t carrying over between sessions. I don’t know if it’s failing to save and is loading default data or if it’s saving successfully but not loading your saves. Maybe I’m missing something that I can’t see.

local dataStore2 = require(1936396537)
dataStore2.Combine("MasterKey", "PlrKills", "PlrDeaths", "PlrTickets", "PlrLastJoin", "PlrPlaylist2", "PlrTools", "PlrSettings")
game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAppearanceLoaded:Wait()
	
	local stats = Instance.new("Folder", player)
	stats.Name = "leaderstats"
	local kills = Instance.new("IntValue", stats)
	kills.Name = "Kills"
	local deaths = Instance.new("IntValue", stats)
	deaths.Name = "Deaths"
	local tickets = Instance.new("IntValue", stats)
	tickets.Name = "Tickets"

	local streak = Instance.new("IntValue", stats)
	streak.Name = "Streak"
	local lastKill = Instance.new("ObjectValue", stats)
	lastKill.Name = "LastKill"
	
	local playerKills = dataStore2("PlrKills", player)
	playerKills:OnUpdate(function(newStat)
		kills.Value = playerKills:Get(newStat)
	end)
	kills.Value = playerKills:Get(0)
	local playerDeaths = dataStore2("PlrDeaths", player)
	playerDeaths:OnUpdate(function(newStat)
		deaths.Value = playerDeaths:Get(newStat)
	end)
	deaths.Value = playerDeaths:Get(0)

	-----------------------------------------------------------------------------------------------------------------------------------

	local playerTickets = dataStore2("PlrTickets", player)
	local playerLastJoin = dataStore2("PlrLastJoin", player)
	
	playerTickets:OnUpdate(function(newTix)
		tickets.Value = newTix
	end)
	tickets.Value = playerTickets:Get(0)
	
	local dailyReward = 0
	local lastReward = playerLastJoin:Get(0)
	if os.time() - lastReward / 3600 > 23 then
		dailyReward = 10
		if player:IsInGroup(9152847) then
			dailyReward = dailyReward * 10
		end
		if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId, 14051142) then
			dailyReward = dailyReward * 10
		end
		
		playerTickets:Increment(dailyReward, 0)
		playerLastJoin:Set(os.time())
	end
	
	-----------------------------------------------------------------------------------------------------------------------------------
	
	local playerPlaylist = dataStore2("PlrPlaylist2", player)
	local playerTools = dataStore2("PlrTools", player)
	local playerSettings = dataStore2("PlrSettings", player)
	
	local myTools = tools
	if IsVIP(player.UserId) then myTools = tools2 end
	game.ReplicatedStorage.UpdatePlayer:FireClient(player, {
		playerPlaylist:Get(songs),
		playerTools:Get(myTools),
		playerSettings:Get({70, -10})
	})

Like, this should work. Meanwhile, I tested the code below in a different game, and it worked perfectly.

local dataStore2 = require(1936396537)

dataStore2.Combine("DATA", "hi", "yo", "holla", "PlrDeaths")
game.Players.PlayerAdded:Connect(function(player)
	local l = Instance.new("Folder", player)
	l.Name = "leaderstats"
	local deaths = Instance.new("IntValue", l)
	deaths.Name = "Deaths"
	
	local playerDeaths = dataStore2("PlrDeaths", player)
	playerDeaths:OnUpdate(function(val)
		deaths.Value = val
	end)
	
	deaths.Value = playerDeaths:Get(0)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			playerDeaths:Increment(1, 0)
		end)
	end)
end)

The “hi, yo, holla” is only there because I had a feeling that it wasn’t working because I was trying to combine multiple keys at once. I was wrong. Anyway, any help would be greatly appreciated. <3

2 Likes

No I don’t think thats the problem because DS2 saves Data itself when the player leaves. Instead of changing the values we update the DataStore key and we use a function OnUpdate which will be fired once the data in the key in updated and it used to update the Int Values as well.

So it worked perfectly in another game?

Correct. In a separate script that tracks when the player dies and who kills them, I even increment the kills and deaths accordingly, and it replicates to the leaderstats thanks to the OnUpdate function in the code above. However, it’s just not saving/loading.

local dataStore2 = require(1936396537)

dataStore2.Combine("DATA", "hi", "yo", "holla", "PlrDeaths")
game.Players.PlayerAdded:Connect(function(player)
	local l = Instance.new("Folder", player)
	l.Name = "leaderstats"
	local deaths = Instance.new("IntValue", l)
	deaths.Name = "Deaths"
	
	local playerDeaths = dataStore2("PlrDeaths", player)
	playerDeaths:OnUpdate(function(val)
		deaths.Value = val
	end)
	
	deaths.Value = playerDeaths:Get(0)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			playerDeaths:Increment(1, 0)
                        print(playerDeaths:Get(0))
		end)
	end)
end)

I added a print statement which will print the updated value of the the Key try this and tell me what is prints.

1 Like

It’s updating and printing the value accordingly.

Why are you creating the same folder and IntValue in 2 scripts

The two chunks of code that I presented are from two different places, one place being the game that I’m working on and the other being the testing place. The code for the testing place is working just fine, but the code in my actual game is not.

Ok then we will now check if its having problem in loading the data in the IntValue

local dataStore2 = require(1936396537)

dataStore2.Combine("DATA", "hi", "yo", "holla", "PlrDeaths")
game.Players.PlayerAdded:Connect(function(player)
	local l = Instance.new("Folder", player)
	l.Name = "leaderstats"
	local deaths = Instance.new("IntValue", l)
	deaths.Name = "Deaths"
	
	local playerDeaths = dataStore2("PlrDeaths", player)
	playerDeaths:OnUpdate(function(val)
	      print(val)
              deaths.Value = val
	end)
	
	deaths.Value = playerDeaths:Get(0)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			playerDeaths:Increment(1, 0)
                        print(deaths.Value)
		end)
	end)
end)

I added 2 prints one in the OnUpdate function and one under the Increment tell me if both of them print the updated value

Yeah, the deaths are incrementing correctly both in the OnUpdate and in the Increment.

So when u leave and rejoin the game the data doesn’t save right?

Right. It just defaults to its original value.

I think the problem is that when the player joins the game the Intvalue isnt being updated to the original Data

local dataStore2 = require(1936396537)

dataStore2.Combine("DATA", "hi", "yo", "holla", "PlrDeaths")
game.Players.PlayerAdded:Connect(function(player)
	local l = Instance.new("Folder", player)
	l.Name = "leaderstats"
	local deaths = Instance.new("IntValue", l)
	deaths.Name = "Deaths"
	
	local playerDeaths = dataStore2("PlrDeaths", player)
	deaths.Value = playerDeaths:Get(0)
       playerDeaths:OnUpdate(function(val)
              deaths.Value = val
	end)
	
	deaths.Value = playerDeaths:Get(0)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			playerDeaths:Increment(1, 0)
		end)
	end)
end)

try this

Oh wait nvm you already have that in your code my bad

1 Like

So its changing the value to the data in the key but lets check if the Data in the key is saved or not

local dataStore2 = require(1936396537)

dataStore2.Combine("DATA", "hi", "yo", "holla", "PlrDeaths")
game.Players.PlayerAdded:Connect(function(player)
	local l = Instance.new("Folder", player)
	l.Name = "leaderstats"
	local deaths = Instance.new("IntValue", l)
	deaths.Name = "Deaths"
	
	local playerDeaths = dataStore2("PlrDeaths", player)
	playerDeaths:OnUpdate(function(val)
		deaths.Value = val
	end)
	
	deaths.Value = playerDeaths:Get(0)
    playerDeaths:Get(0)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			playerDeaths:Increment(1, 0)
		end)
	end)
end)

Paste this code and test the game, update the value and rejoin the game and check if it printed the updated value if not this means its having problem saving the key

b r u h m o m e n t

All I had to do was remove the player.CharacterAppearanceLoaded:Wait() line, and it started working. Though I have no idea why waiting until the player loads in to load the data is a bad thing.

1 Like

ooof
Well I am glad you fixed it

1 Like