DataStore not saving data

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I want to save my leaderstats data.

  1. What is the issue? Include screenshots / videos if possible!

Every time I join, the player is deemed a new player. I’m using the same key to save and grab data.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I’ve searched dev forums and made several posts in Roblox Studio Community

local DSS = game:GetService("DataStoreService")
local Players = game:GetService("Players")

local ZombieStore = DSS:GetDataStore("ZombieDataStore5")

Players.PlayerAdded:Connect(function(player)
	local playerId = player.UserId
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = 'leaderstats'
	leaderstats.Parent = player
	
	local kills = Instance.new("IntValue")
	kills.Name = "Kills"
	kills.Parent = leaderstats
	
	local money = Instance.new("IntValue")
	money.Name = "Money"
	money.Parent = leaderstats
	
	local level = Instance.new("IntValue")
	level.Name = "Level"
	level.Parent = leaderstats
	
	local xp = Instance.new("IntValue")
	xp.Name = "XP"
	xp.Parent = level
	
	local data
	local success, errormessage = pcall(function()
		data = ZombieStore:GetAsync(playerId)
	end)
	
	if success and data ~= nil then
		print("found data!")
		kills.Value = data[1]
		money.Value = data[2]
		level.Value = data[3]
		xp.Value = data[4]
	else
		if data == nil then
			warn("New player, no data found.")
			level.Value = 1
		else
			warn("Data failed to load.")
		end
	end
end)

local function saveData(player)
	local playerId = player.UserId
	local playerStats = player:WaitForChild("leaderstats")

	local dataToStore = {
		playerStats:WaitForChild("Kills").Value,
		playerStats:WaitForChild("Money").Value,
		playerStats:WaitForChild("Level").Value,
		playerStats:WaitForChild("XP").Value
	}

	ZombieStore:SetAsync(playerId, dataToStore)
end

Players.PlayerRemoving:Connect(saveData)

local function onBindToClose()
	for _, player in Players:GetPlayers() do
		print(player)
		task.spawn(saveData, player)
	end
end

game:BindToClose(onBindToClose)

This is a server script in ServerScriptService.

3 Likes

It’s because you’re saving the data in the same index, try this

local DSS = game:GetService("DataStoreService")
local Players = game:GetService("Players")

local ZombieStore = DSS:GetDataStore("ZombieDataStore5")

Players.PlayerAdded:Connect(function(player)
	local playerId = player.UserId
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = 'leaderstats'
	leaderstats.Parent = player
	
	local kills = Instance.new("IntValue")
	kills.Name = "Kills"
	kills.Parent = leaderstats
	
	local money = Instance.new("IntValue")
	money.Name = "Money"
	money.Parent = leaderstats
	
	local level = Instance.new("IntValue")
	level.Name = "Level"
	level.Parent = leaderstats
	
	local xp = Instance.new("IntValue")
	xp.Name = "XP"
	xp.Parent = level
	
	local data
	local success, errormessage = pcall(function()
		data = ZombieStore:GetAsync(playerId)
	end)
	
	if success and data ~= nil then
		print("found data!")
		kills.Value = data[1]
		money.Value = data[2]
		level.Value = data[3]
		xp.Value = data[4]
	else
		if data == nil then
			warn("New player, no data found.")
			level.Value = 1
		else
			warn("Data failed to load.")
		end
	end
end)

local function saveData(player)
	local playerId = player.UserId
	local playerStats = player:WaitForChild("leaderstats")

	local dataToStore = {
		playerStats:WaitForChild("Kills").Value,
		playerStats:WaitForChild("Money").Value,
		playerStats:WaitForChild("Level").Value,
		playerStats:WaitForChild("XP").Value
	}

	ZombieStore:SetAsync(playerId, dataToStore)
end

Players.PlayerRemoving:Connect(saveData)

local function onBindToClose()
	for _, player in Players:GetPlayers() do
		print(player)
		task.spawn(saveData, player)
	end
end

game:BindToClose(onBindToClose)
2 Likes

sorry, that wasn’t in my original code, I was editting something and forgot to change it back. Even so, the script would still detect the player as new

2 Likes

When you’re using GetAsync, you need to put a key there too, you put it in SetAsync, but not in GetAsync, the player has no data to load, because you’re not using the same key, Here is the updated script:

local DSS = game:GetService("DataStoreService")
local Players = game:GetService("Players")

local ZombieStore = DSS:GetDataStore("ZombieDataStore5")

Players.PlayerAdded:Connect(function(player)
	local playerId = player.UserId
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = 'leaderstats'
	leaderstats.Parent = player
	
	local kills = Instance.new("IntValue")
	kills.Name = "Kills"
	kills.Parent = leaderstats
	
	local money = Instance.new("IntValue")
	money.Name = "Money"
	money.Parent = leaderstats
	
	local level = Instance.new("IntValue")
	level.Name = "Level"
	level.Parent = leaderstats
	
	local xp = Instance.new("IntValue")
	xp.Name = "XP"
	xp.Parent = level
	
	local data
	local success, errormessage = pcall(function()
		data = ZombieStore:GetAsync("dataToStore",playerId)
	end)
	
	if success and data ~= nil then
		print("found data!")
		kills.Value = data[1]
		money.Value = data[2]
		level.Value = data[3]
		xp.Value = data[4]
	else
		if data == nil then
			warn("New player, no data found.")
			level.Value = 1
		else
			warn("Data failed to load.")
		end
	end
end)

local function saveData(player)
	local playerId = player.UserId
	local playerStats = player:WaitForChild("leaderstats")

	local dataToStore = {
		playerStats:WaitForChild("Kills").Value,
		playerStats:WaitForChild("Money").Value,
		playerStats:WaitForChild("Level").Value,
		playerStats:WaitForChild("XP").Value
	}

	ZombieStore:SetAsync("dataToStore",playerId)
end

Players.PlayerRemoving:Connect(saveData)

local function onBindToClose()
	for _, player in Players:GetPlayers() do
		print(player)
		task.spawn(saveData, player)
	end
end

game:BindToClose(onBindToClose)

the Key must be in a string, also must be the first parameter in the GetAsync and SetAsync function.

1 Like

the players UserId is the string, and using the same string for everyone makes it so that everyone has the same data

1 Like

You should wrap your SetAsync function in a pcall, also are there any errors, such as ‘data failed to load’ when you load the data