Two Datastore scripts, only one works

So I have 2 datastore scripts, one for leaderstats, and one for stuff I don’t want to be shown in leaderstats.

  1. Leaderstats script
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local TestDataStore = DataStoreService:GetDataStore("DataStoreLeaderstats1")

local data = {
    ["Jumps"] = 0,
    ["Rebirths"] = 0
}

Players.PlayerAdded:Connect(function(Player)
    local DataFromStore = nil

    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = Player

    for name, value in pairs(data) do
        local new = Instance.new("NumberValue")
        new.Name = name
        new.Value = value
        new.Parent = leaderstats
    end

    local s, e = pcall(function()
        DataFromStore = TestDataStore:GetAsync("uid-" .. Player.UserId)
    end)

    if s then
        print("Getting Data For: " .. Player.Name)
        if DataFromStore then
            for name, value in pairs(DataFromStore) do
                Player.leaderstats[name].Value = value
            end
            print("Data Loaded For: " .. Player.Name)
        else
            print("Created New Data For: " .. Player.Name)
        end
    else 
        warn("Error Getting Data For: " .. Player.Name)
    end
end)

Players.PlayerRemoving:Connect(function(Player)
    local DataForStore = {}

    for name, value in pairs(Player.leaderstats:GetChildren()) do
        DataForStore[value.Name] = value.Value
    end

    local success = pcall(TestDataStore.SetAsync, TestDataStore, "uid-" .. Player.UserId, DataForStore)

    if success then
        print("Successfully Saved Data For: " .. Player.Name)
    end
end)
  1. Non-Leaderstats script:
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local TestDataStore = DataStoreService:GetDataStore("DataStoreLeaderstats1")

local datas = {
    ["JumpMultiplier"] = 0,
    ["MDJ"] = 0
}

Players.PlayerAdded:Connect(function(Player)
    local DataFromStore = nil

    local StuffThatIsntInLeaderstats = Instance.new("Folder")
    StuffThatIsntInLeaderstats.Name = "StuffThatIsntInLeaderstats"
    StuffThatIsntInLeaderstats.Parent = Player

    for name, value in pairs(datas) do
        local new = Instance.new("NumberValue")
        new.Name = name
        new.Value = value
        new.Parent = StuffThatIsntInLeaderstats
    end

    local s, e = pcall(function()
        DataFromStore = TestDataStore:GetAsync("uid-" .. Player.UserId)
    end)

    if s then
        print("Getting Data For: " .. Player.Name)
        if DataFromStore then
            for name, value in pairs(DataFromStore) do
                Player.StuffThatIsntInLeaderstats[name].Value = value
            end
            print("Data Loaded For: " .. Player.Name)
        else
            print("Created New Data For: " .. Player.Name)
        end
    else 
        warn("Error Getting Data For: " .. Player.Name)
    end
end)

Players.PlayerRemoving:Connect(function(Player)
    local DataForStore = {}

    for name, value in pairs(Player.StuffThatIsntInLeaderstats:GetChildren()) do
        DataForStore[value.Name] = value.Value
    end

    local success = pcall(TestDataStore.SetAsync, TestDataStore, "uid-" .. Player.UserId, DataForStore)

    if success then
        print("Successfully Saved Data For: " .. Player.Name)
    end
end)

What do you mean only one works? Which script doesn’t work? What is the error/problem you are encountering? Please be more clear and specific when asking the people on devforum about your issue so we can help you better.

Oops, I accidentally left that out. The one that isn’t meant to show in leaderstats doesn’t save its values. And I’ve made sure all values are controlled by server side scripts, so it isn’t the way I’m adding the values.

(I’d like to note, the stats do change, they just don’t save.)

Why couldn’t you just combine the 2 scripts into 1…?

The reason why it isn’t working is because SetAsync overwrites the current key you’re trying to save, and if you’re calling it twice from 2 different scripts then only 1 of those values will save

Here’s a better rendition of what I assume you’re trying to achieve here:

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local TestDataStore = DataStoreService:GetDataStore("DataStoreLeaderstats1")

local hiddendatas = {
    ["JumpMultiplier"] = 0,
    ["MDJ"] = 0
}

local datas {
    ["Jumps"] = 0,
    ["Rebirths"] = 0
}

Players.PlayerAdded:Connect(function(Player)
    local DataFromStore = nil

    local StuffThatIsntInLeaderstats = Instance.new("Folder")
    StuffThatIsntInLeaderstats.Name = "StuffThatIsntInLeaderstats"
    StuffThatIsntInLeaderstats.Parent = Player

    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = Player

    for name, value in pairs(datas) do
        local new = Instance.new("NumberValue")
        new.Name = name
        new.Value = value
        new.Parent = leaderstats
    end
	
    for name, value in pairs(hiddendatas) do
        local new = Instance.new("NumberValue")
        new.Name = name
        new.Value = value
        new.Parent = StuffThatIsntInLeaderstats
    end

    local s, e = pcall(function()
        DataFromStore = TestDataStore:GetAsync("uid-" .. Player.UserId)
    end)

    if s then
        print("Getting Data For: " .. Player.Name)
		
		if DataFromStore ~= nil then
			for name, value in pairs(DataFromStore) do
				local ObjectCheck = StuffThatIsntInLeaderstats:FindFirstChild(name)
				
				if ObjectCheck ~= nil then
					ObjectCheck.Value = value
				end
			end
			
			print("Data Loaded For: " .. Player.Name)
        else
            print("Created New Data For: " .. Player.Name)
        end
    else 
        warn("Error Getting Data For: " .. Player.Name)
    end
end)

Players.PlayerRemoving:Connect(function(Player)
    local DataForStore = {}
	local HiddenItems = Player:WaitForChild("StuffThatIsntInLeaderstats")
	local leaderstats = Player:WaitForChild("leaderstats")
	
    for name, value in pairs(HiddenItems:GetChildren()) do
        DataForStore[value.Name] = value.Value
    end

    for name, value in pairs(leaderstats:GetChildren()) do
        DataForStore[value.Name] = value.Value
    end
	
    local success, whoops = pcall(function()
		TestDataStore:SetAsync("uid-"..Player.UserId, DataForStore)
	end)
	
    if success then
        print("Successfully Saved Data For: " .. Player.Name)
    end
end)

I haven’t tested this, so there may be some errors

Now neither of them work for some odd reason, what the heck?

Try this one instead, I knew I messed something up unfortunately

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local TestDataStore = DataStoreService:GetDataStore("DataStoreLeaderstats1")

local hiddendatas = {
	["JumpMultiplier"] = 0,
	["MDJ"] = 0
}

local datas = {
	["Jumps"] = 0,
	["Rebirths"] = 0
}

Players.PlayerAdded:Connect(function(Player)
	local DataFromStore = nil

	local StuffThatIsntInLeaderstats = Instance.new("Folder")
	StuffThatIsntInLeaderstats.Name = "StuffThatIsntInLeaderstats"
	StuffThatIsntInLeaderstats.Parent = Player

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = Player

	for name, value in pairs(datas) do
		local new = Instance.new("NumberValue")
		new.Name = name
		new.Value = value
		new.Parent = leaderstats
	end

	for name, value in pairs(hiddendatas) do
		local new = Instance.new("NumberValue")
		new.Name = name
		new.Value = value
		new.Parent = StuffThatIsntInLeaderstats
	end

	local s, e = pcall(function()
		DataFromStore = TestDataStore:GetAsync("uid-" .. Player.UserId)
	end)

	if s then
		print("Getting Data For: " .. Player.Name)

		if DataFromStore ~= nil then
			for name, value in pairs(DataFromStore) do
				local ObjectCheck = StuffThatIsntInLeaderstats:FindFirstChild(name) or leaderstats:FindFirstChild(name)

				if ObjectCheck ~= nil then
					ObjectCheck.Value = value
				end
			end

			print("Data Loaded For: " .. Player.Name)
		else
			print("Created New Data For: " .. Player.Name)
		end
	else 
		warn("Error Getting Data For: " .. Player.Name)
	end
end)

Players.PlayerRemoving:Connect(function(Player)
	local DataForStore = {}
	local HiddenItems = Player:WaitForChild("StuffThatIsntInLeaderstats")
	local leaderstats = Player:WaitForChild("leaderstats")

	for name, value in pairs(HiddenItems:GetChildren()) do
		DataForStore[value.Name] = value.Value
	end

	for name, value in pairs(leaderstats:GetChildren()) do
		DataForStore[value.Name] = value.Value
	end
		
	local success, whoops = pcall(function()
		TestDataStore:SetAsync("uid-"..Player.UserId, DataForStore)
	end)

	if success then
		print("Successfully Saved Data For: " .. Player.Name)
	end
end)

Also, make sure that API Services are toggled on within your Game Settings, otherwise the DataStoreService won’t properly load in correctly

Oh sorry, I wasn’t responding to the code you put, devforum wasn’t updating in real time so I thought you were doing something else. I tried to fix my code on my own and it broke both of them :skull: thanks for the help and I’ll let you know if it works.

Unfortunately, this didn’t work. I made sure I corrected anything that gave me errors and it still didn’t work.

(And I made sure api access was on)

Are you sure though? Did you make sure to try the script I just posted out? It completely worked fine on my side, so I don’t know why it wouldn’t work for you:

I would recommend checking out your console Output, and see what outputted back and also try it on an entirely new place, as I replicated the same code that I originally sent (After fixing) and it worked for me

Not sure if this is on purpose, but you are using the same datastore for both scripts. If that is on purpose, you are also using the same key.

Combine both your scripts into 1 and paste this inside:

--//Services
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

--//Variables
local TestDataStore = DataStoreService:GetDataStore("DataStoreLeaderstats1")

--//Tables
local data = {
	Jumps = 0,
	Rebirths = 0
}

local datas = {
	JumpMultiplier = 0,
	MDJ = 0
}

--//Functions
Players.PlayerAdded:Connect(function(Player)
	local DataFromStore = nil

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = Player
	
	local StuffThatIsntInLeaderstats = Instance.new("Folder")
	StuffThatIsntInLeaderstats.Name = "StuffThatIsntInLeaderstats"
	StuffThatIsntInLeaderstats.Parent = Player

	for name, value in pairs(data) do
		local new = Instance.new("NumberValue")
		new.Name = name
		new.Value = value
		new.Parent = leaderstats
	end
	
	for name, value in pairs(datas) do
		local new = Instance.new("NumberValue")
		new.Name = name
		new.Value = value
		new.Parent = StuffThatIsntInLeaderstats
	end

	local success, errorMessage = pcall(function()
		DataFromStore = TestDataStore:GetAsync("uid-" .. Player.UserId)
	end)

	if success then
		print("Getting Data For: " .. Player.Name)
		
		if DataFromStore then
			for name, value in pairs(DataFromStore) do
				local valueBase = Player.StuffThatIsntInLeaderstats:FindFirstChild(name)
				
				if valueBase then
					valueBase.Value = value
				end
			end
			
			for name, value in pairs(DataFromStore) do
				local valueBase = Player.leaderstats:FindFirstChild(name)

				if valueBase then
					valueBase.Value = value
				end
			end
			
			print("Data Loaded For: ".. Player.Name)
		else
			print("Created New Data For: ".. Player.Name)
		end
	else 
		warn("Error Getting Data For: ".. Player.Name)
		warn(errorMessage)
	end
end)

Players.PlayerRemoving:Connect(function(Player)
	local DataForStore = {}

	for i, value in ipairs(Player.leaderstats:GetChildren()) do
		DataForStore[value.Name] = value.Value
	end
	
	for i, value in ipairs(Player.StuffThatIsntInLeaderstats:GetChildren()) do
		DataForStore[value.Name] = value.Value
	end

	local success, errorMessage = pcall(TestDataStore.SetAsync, TestDataStore, "uid-".. Player.UserId, DataForStore)
	
	if success then
		print("Successfully Saved Data For: " .. Player.Name)
	else
		warn(errorMessage)
	end
end)

No console output, same code in ServerScriptService on a server script. I changed the value with the dev console so I can say 100% that it doesn’t work for me for whatever reason. I can try doing it on a new place. (I am testing the HiddenData, the Jumps and Rebirths work.)

Try this? Maybe it will work, maybe not.

It’s because he forgot to also include those when setting the values saved. Mine already does that, so you should try it.

Yeah I tried that solution earlier, didn’t work.

Maybe try combining the scripts? I don’t see why you need 2 scripts for the same datastore AND key?

Trying yours, it looks like only half works. It saves sometimes and sometimes it doesn’t.

Have you tried ingame? I added BindToClose which fires when the server shutdowns so it bugs in studio.

I’m going to try, but my computer runs on a potato so give me a second ;-;

Okay lol, take your time.