local DataStoreService = game:GetService("DataStoreService")
local MyDSS = DataStoreService:GetDataStore("MyDataStore")
game.Players.PlayerAdded:Connect(function(Player)
local data
local success, errorMessage = pcall(function()
data = MyDSS:GetAsync(Player.UserId.."-Kills")
end)
if success then
Player.leaderstats.Kills.Value = data
Player.leaderstats.Deaths.Value = data
else
print("There was an error whilst getting your data!")
warn(errorMessage)
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local success, errorMessage = pcall(function()
MyDSS:SetAsync(Player.UserId.."-Kills",Player.leaderstats.Kills.Value, "-Deaths", Player.leaderstats.Deaths.Value)
end)
if success then
print("Data Has Been Succcessfully Saved!")
else
print("There was an error when saving data!")
warn(errorMessage)
end
end)
Please Help Me,
Your Fellow Developer,
The Spikey Man
You can still do that. You can use a different key, or you can keep the same key. However, you can’t use commas to separate them, you would have to put quotation marks around the comma to separate them.
local DataStoreService = game:GetService("DataStoreService")
local MyDSS = DataStoreService:GetDataStore("MyDataStore")
game.Players.PlayerAdded:Connect(function(Player)
local data, data2
local success, errorMessage = pcall(function()
data = MyDSS:GetAsync(Player.UserId.."-Kills")
data2 = MyDSS:GetAsync(Player.UserId.."-Deaths")
end)
if success then
Player.leaderstats.Kills.Value = data
Player.leaderstats.Deaths.Value = data2
else
print("There was an error whilst getting your data!")
warn(errorMessage)
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local success, errorMessage = pcall(function()
MyDSS:SetAsync(Player.UserId.."-Kills",Player.leaderstats.Kills.Value)
MyDSS:SetAsync(Player.UserId.."-Deaths",Player.leaderstats.Deaths.Value)
end)
if success then
print("Data Has Been Succcessfully Saved!")
else
print("There was an error when saving data!")
warn(errorMessage)
end
end)
I would highly recommend not using tutorials for DataStores, as many people explain them very poorly, and people end up with poorly scripted DataStores.
Also, if youre going to be saving multiple values to the same DataStore, do not use different keys.
Instead, use a table (A list, to make it simple) of the values youre saving.
The reason you’ll want to do this, and not just different save keys is because DataStores have limits on them, sending too many requests to save data will cause them to error, and not save the data.
Anyway, this should fix the problem:
local DataStoreService = game:GetService("DataStoreService")
local MyDSS = DataStoreService:GetDataStore("MyDataStore")
game.Players.PlayerAdded:Connect(function(Player)
local data
local success, errorMessage = pcall(function()
data = MyDSS:GetAsync(Player.UserId)
end)
-- You'll need to create the values if they dont already exist.
local Leaderstats = Instance.new("Folder", Player)
Leaderstats.Name = 'leaderstats'
local Kills = Instance.new("IntValue", Leaderstats)
Kills.Name = 'Kills'
local Deaths = Instance.new("IntValue", Leaderstats)
Deaths.Name = 'Deaths'
-- If you have any other scripts creating these values, delete those scripts.
if success and data ~= nil then
Kills.Value = data.Kills
Deaths.Value = data.Deaths
elseif success and data == nil then
print('No saved data was found')
else
print("There was an error whilst getting your data!")
warn(errorMessage)
end
end)
function SaveData(Player)
-- Create a table to save all your values to
local DataToSave = {}
-- Look through the leaderstats folder, and insert the values into the table that just got created
for i, v in pairs(Player.leaderstats:GetChildren()) do
DataToSave[v.Name] = v.Value
end
-- Finally, save to the DataStore.
MyDSS:SetAsync(Player.UserId, DataToSave)
end
game.Players.PlayerRemoving:Connect(SaveData)