DataStore script not saving

Hello,
I have this script here that is meant to be saving data but its not and I cant figure out the problem.

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("MoneyStats")
local rs = game.ReplicatedStorage

	local PetData = Instance.new("Folder", rs)
	PetData.Name = "PetData"
	local Pet1= Instance.new("IntValue", PetData)
	Pet1.Name = "Pet1"
	Pet1.Value = 0
	local Kills= Instance.new("IntValue", PetData)
	Kills.Name = "Kills"
	Kills.Value = 0

local Data = DataStore:GetAsync("PetData1")
	if Data then
		Pet1.Value = Data.Pet1
		Kills.Value = Data.Kills 
	end

game:BindToClose(
	print("Data saved"),
	DataStore:SetAsync("PetData1", {
		["Pet1"] = rs.PetData.Pet1.Value;
		["Kills"] = rs.PetData.Kills.Value;
}))

local Data = DataStore:GetAsync("PetData1")
	if Data then
		Pet1.Value = Data["Pet1"]
		Kills.Value = Data["Kills"]
	end
2 Likes

I already fixed it, But thank you ill implment this to my code.

1 Like

How did you fix it? I’ve been trying to make a leaderstats with multiple values, but still haven’t found the solution.

1 Like

I had the BindToClose function messed up, heres my new code.

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("MoneyStats")
local rs = game.ReplicatedStorage

	local PetData = Instance.new("Folder", rs)
	PetData.Name = "PetData"
	local Pet1= Instance.new("IntValue", PetData)
	Pet1.Name = "Pet1"
	Pet1.Value = 0
	local Kills= Instance.new("IntValue", PetData)
	Kills.Name = "Kills"
	Kills.Value = 0

local Data = DataStore:GetAsync("PetData1")
if Data then
	print("Loaded Data!")
		Pet1.Value = Data.Pet1
		Kills.Value = Data.Kills 
	end

game:BindToClose(function()
	print("Saved Data!")
	
	DataStore:SetAsync("PetData1", {
		["Pet1"] = rs.PetData.Pet1.Value;
		["Kills"] = rs.PetData.Kills.Value;
	})
end)
1 Like

oh wait nvm this is quite useless ig

the way that you loaded the data is fine ig

1 Like

Thanks for your input anyway :slight_smile:

1 Like

SetAsync() is missing the key argument.

There is a-lot going on with this script. Is it for the player or for the server to keep track of stats?

1 Like

Server, And I found the issue and the fix.