Hello everyone! I hope you all had a wonderful Christmas! I am currently figuring out how to make the default values something else for ever value.
I want the values to be:
Level = 1
Current = 0
Max = 100
The Script
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local LevelingDataStore = DataStoreService:GetDataStore("DSTEST0002")
local TIME_TRIALS = {
"Current",
"Max",
"Level"
}
local function playerAdded(player)
local levelFolder = Instance.new("Folder")
levelFolder.Name = "LevelsSystem"
local createdValues = {}
for _, timeTrial in ipairs(TIME_TRIALS) do
value = Instance.new("IntValue")
value.Name = timeTrial
value.Value = 0
createdValues[timeTrial] = value
value.Parent = levelFolder
end
local success, result = pcall(function ()
return LevelingDataStore:GetAsync(player.UserId)
end)
if success then
if result then
for trial, completionTime in pairs(result) do
if not createdValues[trial] then continue end
createdValues[trial].Value = completionTime
end
end
else
warn("Data could not be loaded for user " .. tostring(player.UserId))
end
levelFolder.Parent = player
value.Changed:Connect(function()
LevelingDataStore:SetAsync(player.UserId, value.Value)
end)
end
local function playerRemoving(player)
local levelFolder = player:FindFirstChild("LevelsSystem")
if not levelFolder then return end
local values = {}
for _, levelValue in ipairs(levelFolder:GetChildren()) do
values[levelValue.Name] = levelValue.Value
end
local success = pcall(function ()
return LevelingDataStore:SetAsync(player.UserId, values)
end)
end
Players.PlayerAdded:Connect(playerAdded)
Players.PlayerRemoving:Connect(playerRemoving)
for _, player in ipairs(Players:GetPlayers()) do
playerAdded(player)
end
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local LevelingDataStore = DataStoreService:GetDataStore("DSTEST0002")
local TIME_TRIALS = {
"Current",
"Max",
"Level"
}
local function playerAdded(player)
local levelFolder = Instance.new("Folder")
levelFolder.Name = "LevelsSystem"
local createdValues = {}
for _, timeTrial in ipairs(TIME_TRIALS) do
value = Instance.new("IntValue")
value.Name = timeTrial
value.Value = 0
createdValues[timeTrial] = value
value.Parent = levelFolder
end
local success, result = pcall(function ()
return LevelingDataStore:GetAsync(player.UserId)
end)
if success then
if result then
for trial, completionTime in pairs(result) do
if not completionTime then
if trial == "Level" then completionTime = 1 end
if trial == "Max" then completionTime = 100 end
if trial == "Current" then completionTime = 0 end
end
createdValues[trial].Value = completionTime
end
end
else
warn("Data could not be loaded for user " .. tostring(player.UserId))
end
levelFolder.Parent = player
value.Changed:Connect(function()
LevelingDataStore:SetAsync(player.UserId, value.Value)
end)
end
local function playerRemoving(player)
local levelFolder = player:FindFirstChild("LevelsSystem")
if not levelFolder then return end
local values = {}
for _, levelValue in ipairs(levelFolder:GetChildren()) do
values[levelValue.Name] = levelValue.Value
end
local success = pcall(function ()
return LevelingDataStore:SetAsync(player.UserId, values)
end)
end
Players.PlayerAdded:Connect(playerAdded)
Players.PlayerRemoving:Connect(playerRemoving)
for _, player in ipairs(Players:GetPlayers()) do
playerAdded(player)
end
I’m pretty sure this is what you want and should work, basically I just changed the if not data value thingy
I’m on mobile right now so if it’s messed up I’m sorry
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local LevelingDataStore = DataStoreService:GetDataStore("DSTEST0002")
local TIME_TRIALS = {
"Current",
"Max",
"Level"
}
local function playerAdded(player)
local levelFolder = Instance.new("Folder")
levelFolder.Name = "LevelsSystem"
local createdValues = {}
for _, timeTrial in ipairs(TIME_TRIALS) do
value = Instance.new("IntValue")
value.Name = timeTrial
value.Value = 0
createdValues[timeTrial] = value
value.Parent = levelFolder
end
local success, result = pcall(function ()
return LevelingDataStore:GetAsync(player.UserId)
end)
if success then
if result then
for trial, completionTime in pairs(result) do
if not completionTime then
if trial == "Level" then completionTime = 1 end
if trial == "Max" then completionTime = 100 end
if trial == "Current" then completionTime = 0 end
end
createdValues[trial].Value = completionTime
end
end
else
warn("Data could not be loaded for user " .. tostring(player.UserId))
end
levelFolder.Parent = player
end
local function playerRemoving(player)
local levelFolder = player:FindFirstChild("LevelsSystem")
if not levelFolder then return end
local values = {}
for _, levelValue in ipairs(levelFolder:GetChildren()) do
values[levelValue.Name] = levelValue.Value
end
local success, err = pcall(function ()
LevelingDataStore:SetAsync(player.UserId, values)
end)
if err then
warn(err)
end
end
Players.PlayerAdded:Connect(playerAdded)
Players.PlayerRemoving:Connect(playerRemoving)
for _, player in ipairs(Players:GetPlayers()) do
playerAdded(player)
end
This includes a small fix, it mainly has to do with the value.changed function which would basically reset stats, If I broke it more I am sorry
I’ve also added an error message for the saving sequence to see if there was an error saving (edit the error message is now working I did it wrong)
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local LevelingDataStore = DataStoreService:GetDataStore("DSTEST0002")
local TIME_TRIALS = {
"Current",
"Max",
"Level"
}
local function playerAdded(player)
local levelFolder = Instance.new("Folder")
levelFolder.Name = "LevelsSystem"
local createdValues = {}
for _, timeTrial in ipairs(TIME_TRIALS) do
value = Instance.new("IntValue")
value.Name = timeTrial
value.Value = 0
createdValues[timeTrial] = value
value.Parent = levelFolder
end
local success, result = pcall(function ()
return LevelingDataStore:GetAsync(player.UserId)
end)
if success then
if result then
for trial, completionTime in pairs(result) do
if not completionTime then
if trial == "Level" then completionTime = 1 end
if trial == "Max" then completionTime = 100 end
if trial == "Current" then completionTime = 0 end
end
createdValues[trial].Value = completionTime
end
else
createdValues["Max"]. Value = 100
createdValues ["Current"].Value = 0
createdValues["Level"].Value = 1
end
else
warn("Data could not be loaded for user " .. tostring(player.UserId))
end
levelFolder.Parent = player
end
local function playerRemoving(player)
local levelFolder = player:FindFirstChild("LevelsSystem")
if not levelFolder then return end
local values = {}
for _, levelValue in ipairs(levelFolder:GetChildren()) do
values[levelValue.Name] = levelValue.Value
end
local success, err = pcall(function ()
LevelingDataStore:SetAsync(player.UserId, values)
end)
if err then
warn(err)
end
end
Players.PlayerAdded:Connect(playerAdded)
Players.PlayerRemoving:Connect(playerRemoving)
for _, player in ipairs(Players:GetPlayers()) do
playerAdded(player)
end