How can I make the default value different things for every datastore?

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

If you can help, please let me know. Thanks, WE

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

Well for some reason, if you leave and re-join, the folder is not in the player, and it doesn’t work. It just stays 0.

Well I don’t believe I touched that part of the script, also I’ve edited my solution so you may want to look at that now

image
Still 0.
Leave and re-join, it is still there.

Hmm I’m going to look over the script again and look for anything that might have been wrong

1 Like
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)

err is an unknown global in this script.
image
And end waslalso forgotton
image

but the values are still 0.

Yea that was me messing up the error message at the end mb, I’ve gone ahead and fixed the err part
Edit and now the end too

Also this solution I just realized won’t work for players with no data so I’ll fix that too

Ok the values are still 0, and there is no errors coming out.

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

This should be fixed I hope