You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
I want to save my leaderstats data.
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.
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)
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)
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
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.