DataStore2 isn't loading data/saving?

Here’s the script, I believe I’ve done everything correct but may be mistaken,
All the Bounty, Information etc is loaded, it just isn’t saving.

Thanks in advanced!

local DataStore2 = require(game.ServerScriptService.DataStore2)
DataStore2.Combine("MasterKey7", "Bounty")

game.Players.PlayerAdded:Connect(function(Player)
	local Information = Player:WaitForChild("Information")
	local Bounty = Player:WaitForChild("leaderstats"):WaitForChild("Bounty")
	local BountyData = DataStore2("Bounty", Player)

	if BountyData:Get() == nil then
		Bounty.Value = 0
	else
		Bounty.Value = BountyData:Get()
	end

	print(BountyData:Get())

end)

game.Players.PlayerRemoving:Connect(function(Player)
	local BountyData = DataStore2("Bounty", Player)
	local Bounty = Player:WaitForChild("leaderstats"):WaitForChild("Bounty")

	BountyData:Set(Bounty.Value)
end)

Hey, anyone with the issue. I fixed it.
Changed it to save every time it’s updated instead of when you’re leaving.

That might be a bit excessive, perhaps you just needed to add the BindToClose() callback if you were testing in studio.

1 Like

I like that, I’ll definitely add that. I completely forgot that existed, Thank you.

BindToClose is also super useful for when a game crashes or etc because it will save the data before the connection is lost so that players won’t lose data because PlayerRemoving was never ran before they disconnected.

Ohhh, okay that makes sense. Awesome I gotcha