Stats not saving

local DataStore2 = require(1936396537)
DataStore2:Combine("MasterKey", "Points")

local DefaultValue = 0

game.Players.PlayerAdded:Connect(function(player)
	local PointsDataStore = DataStore2("Points", player)
	
    local Points = Instance.new("IntValue", player)
    Points.Name = "Points"
    If PointsDatastore:Get() ~= nil then
            Points.Value = PointsDatastore:Get()
   else
          Points.Value = dafaultValue
    end

	Points.Changed:Connect(function()
		Points.Value:Set(PointsDataStore)
	end)
end)

There are a number of issues with the current code you have. First off, why are you using multiple datastores? Wouldn’t it be better to use one main one instead? Secondly, you aren’t using any pcalls whatsoever, so if something goes wrong, your whole script breaks and becomes obselete.

I cleaned up your code and made some changes to how it functions. However I can not guarantee this will work:

local DataStoreService = game:GetService("DataStoreService") --Get Datastore
local MainDatastore = DataStoreService:GetDataStore("MainDatastore") --Instead of having multiple datastores, why not use one!
local defaultData = {}
local DataTable = {}
for index, value in pairs(script:GetChildren()) do
	defaultData[value.Name] = value.Value --Creating a preset table
end
game.Players.PlayerAdded:Connect(function(player) --Fetch the player
	local fetchedData
	local success, errormessage = pcall(function()
		fetchedData = MainDatastore:GetAsync(player.UserId) --Using a pcall (error-handling) to fetch it in case of an error
	end)
	if success then ---Check if successful
		for Index, Value in pairs(fetchedData)do
			local NewValue = Instance.new("NumberValue") --Instancing number value
			NewValue.Name = Index --Setting name to the index of the fetched data
			NewValue.Value =  Value or defaultData[Index]
			NewValue.Parent = player --Better to parent it later than as a second paramter
		end
	else
		warn(errormessage) --Show us the error.
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	for i,v in pairs(script:GetChildren()) do 
		DataTable[v.Name] = v.Value
	end
	MainDatastore:SetAsync(player.UserId, DataTable)
end)

How do I make new values with this script?

What do you mean? This does what you originally said coded it to do which is:

  1. Loop through all the script’s number values
  2. Set those values to whatever was fetched/ the preset number if nil
  3. When the player leaves, it will save the table to the datastore to restart the cycle again

Ohhhh I get it. I put number values inside the script and it gives me this error:

ServerScriptService.Script:14: invalid argument #1 to ‘pairs’ (table expected, got nil)

This means theres no data so you should check if there is data in the plr before setting the values

Ah I know why. I forgot to check if the data fetched wasn’t empty, let me fix that rq

local DataStoreService = game:GetService("DataStoreService") --Get Datastore
local MainDatastore = DataStoreService:GetDataStore("MainDatastore") --Instead of having multiple datastores, why not use one!
local defaultData = {}
local DataTable = {}
for index, value in pairs(script:GetChildren()) do
	defaultData[value.Name] = value.Value --Creating a preset table
end
game.Players.PlayerAdded:Connect(function(player) --Fetch the player
	local fetchedData
	local success, errormessage = pcall(function()
		fetchedData = MainDatastore:GetAsync(player.UserId) --Using a pcall (error-handling) to fetch it in case of an error
	end)
	if success then ---Check if successful
		if fetchedData ~= nil then --Check if data is not nil
			for Index, Value in pairs(fetchedData)do
				local NewValue = Instance.new("NumberValue") --Instancing number value
				NewValue.Name = Index --Setting name to the index of the fetched data
				NewValue.Value =  Value or defaultData[Index]
				NewValue.Parent = player --Better to parent it later than as a second paramter
			end
		end
	else
		warn(errormessage) --Show us the error.
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	for i,v in pairs(script:GetChildren()) do 
		DataTable[v.Name] = v.Value
	end
	MainDatastore:SetAsync(player.UserId, DataTable)
end)

Okay, now there is no error but it doesn’t make any values.

Oh damn it. I’ve done it again. Let me sort it out rq

local DataStoreService = game:GetService("DataStoreService") --Get Datastore
local MainDatastore = DataStoreService:GetDataStore("MainDatastore") --Instead of having multiple datastores, why not use one!
local defaultData = {}
local DataTable = {}
for index, value in pairs(script:GetChildren()) do
	defaultData[value.Name] = value.Value --Creating a preset table
end
game.Players.PlayerAdded:Connect(function(player) --Fetch the player
	local fetchedData
	local success, errormessage = pcall(function()
		fetchedData = MainDatastore:GetAsync(player.UserId) --Using a pcall (error-handling) to fetch it in case of an error
	end)
	if success then ---Check if successful
		if fetchedData ~= nil then
			for Index, Value in pairs(fetchedData)do
				local NewValue = Instance.new("NumberValue") --Instancing number value
				NewValue.Name = Index --Setting name to the index of the fetched data
				NewValue.Value =  Value or defaultData[Index]
				NewValue.Parent = player --Better to parent it later than as a second paramter
			end
		else
			for index, value in pairs(defaultData) do
				local Value = Instance.new("NumberValue")
				Value.Name = index
				Value.Parent = player
				Value.Value = value
			end
		end
	else
		warn(errormessage) --Show us the error.
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	for i,v in pairs(script:GetChildren()) do 
		DataTable[v.Name] = v.Value
	end
	MainDatastore:SetAsync(player.UserId, DataTable)
end)

@MajinBluee this should be alr

Did you try my code? If you did were there errors?

This doesn’t save the values tho :confused:

I didn’t try your code so I don’t know if there were errors

Sorry I messed up again. I don’t know why this keeps happening lol let me fix it I see the issue

local DataStoreService = game:GetService("DataStoreService") --Get Datastore
local MainDatastore = DataStoreService:GetDataStore("MainDatastore") --Instead of having multiple datastores, why not use one!
local defaultData = {}
local DataTable = {}
for index, value in pairs(script:GetChildren()) do
	defaultData[value.Name] = value.Value --Creating a preset table
end
game.Players.PlayerAdded:Connect(function(player) --Fetch the player
	local fetchedData
	local success, errormessage = pcall(function()
		fetchedData = MainDatastore:GetAsync(player.UserId) --Using a pcall (error-handling) to fetch it in case of an error
	end)
	if success then ---Check if successful
		if fetchedData ~= nil then --Check if data is not nil
			for Index, Value in pairs(fetchedData)do
				local NewValue = Instance.new("NumberValue") --Instancing number value
				NewValue.Name = Index --Setting name to the index of the fetched data
				NewValue.Value =  Value or defaultData[Index]
				NewValue.Parent = player --Better to parent it later than as a second paramter
			end
		end
	else
		warn(errormessage) --Show us the error.
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	for i,v in pairs(player:GetChildren()) do 
		if v:IsA("NumberValue") then
			DataTable[v.Name] = v.Value
		end
	end
	local success, errormessage = pcall(function()
		MainDatastore:SetAsync(player.UserId, DataTable)
	end)
	
end)

Alr alr. Hopefully this will be the working version

Maybe you could try it? I have been using Ds2 for a long time and it has never let me down

Nope. Doesn’t work like always :weary:

I don’t really know how to use Datastore2 tho

Hm. So it doesn’t work. I’ll try hook a :BindToClose() function and see if that can solve the case