Hello everyone. I am trying to use a DataStore 2 leaderstats to put in my leaderboard but since I combined two leaderstats in one script, it does not allow me to do that, and it only lets me use it on the Gold but not Level leaderstats. Why is that?
local DataStore2 = require(1936396537)
DataStore2.Combine("MasterKey","Gold","Level")
game.Players.PlayerAdded:Connect(function(plr)
local datastores = {
["Gold"] = DataStore2("Gold", plr),
["Level"] = DataStore2("Level", plr)
}
local datas = {
["Gold"] = datastores["Gold"]:Get(),
["Level"] = datastores["Level"]:Get()
}
local folder = Instance.new("Folder",plr)
folder.Name = "leaderstats"
local gold = Instance.new("IntValue", folder)
gold.Name = "Gold"
local level = Instance.new("NumberValue", folder)
level.Name = "Level"
if datas["Gold"] ~= nil then
gold.Value = datas["Gold"]
else
gold.Value = 100
end
if datas["Level"] ~= nil then
level.Value = datas["Level"]
else
level.Value = 0
end
gold.Changed:Connect(function()
datastores["Gold"]:Set(gold.Value)
end)
level.Changed:Connect(function()
datastores["Level"]:Set(level.Value)
end)
end)
when player join your game, Server will get player’s data from DataStore
when player leave your game, Server will save player’s data
I made this example from before :
local dss = game:GetService("DataStoreService")
local DataStoreToKeep = dss:GetDataStore("Player DataStore")
-- Get Player MONEY Data
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local money = Instance.new("IntValue")
money.Name = "MONEY"
money.Parent = leaderstats
local data
local success, errormessage = pcall(function()
data = DataStoreToKeep:GetAsync(player.UserId.."_money")
end)
if success then
wait(0.2)
player.leaderstats.MONEY.Value = data
else
warn(errormessage)
end
end)
-- Save player MONEY Data When Leave The Server
game.Players.PlayerRemoving:Connect(function(player)
local data
local success, errormessage = pcall(function()
data = DataStoreToKeep:SetAsync(player.UserId.."_money", player.leaderstats.MONEY.Value)
end)
if success then
player.leaderstats.MONEY.Value = data
else
warn(errormessage)
end
end)
That is not the problem. Two datastores are connected that iswhy the “Global Leaderboard” I tried adding to will not work. I think I know why. The leaderboard does not support it when it is connected and is confused.