Hello, I want to create a DataStore using Data Store V2, the issue is that the Data Store saves, but it always gets the default Value.
This is my code:
local DataStoreService = game:GetService('DataStoreService')
local Players = game:GetService('Players')
local options = Instance.new('DataStoreOptions')
options:SetExperimentalFeatures({["v2"] = true})
--options.AllScopes = true
local PointsData = DataStoreService:GetDataStore("PlayerPoints", "global", options)
local LevelData = DataStoreService:GetDataStore("PlayerLevel", "global", options)
local SetOptions = Instance.new('DataStoreSetOptions')
SetOptions:SetMetadata({['PlayerPointsType'] = 'Int'})
SetOptions:SetMetadata({['PlayerLevelType'] = 'Int'})
local function SaveStatsData(Player)
local Points = Player:WaitForChild("leaderstats").Points.Value
local Level = Player:WaitForChild("leaderstats").Level.Value
local PlayerPointsKey = Player.UserId
local Sucess, Errormessage = pcall(function()
PointsData:SetAsync(PlayerPointsKey, Points, {Player.UserId}, SetOptions)
PointsData:SetAsync(PlayerPointsKey, Level, {Player.UserId}, SetOptions)
end)
if not Sucess then
print(Errormessage)
else
print('Data Saved')
end
end
local function LoadPlayerPointsData(Player)
local PlayerPointsKey = Player.UserId
local Sucess, CurrentNumber, KeyInfo:DataStoreKeyInfo = pcall(function()
return PointsData:GetAsync(PlayerPointsKey, Player:WaitForChild("leaderstats").Points.Value)
end)
if Sucess then
print("Points:"..CurrentNumber)
print("Points:"..KeyInfo.Version)
print("Points"..KeyInfo.CreatedTime)
print("Points:"..KeyInfo.UpdatedTime)
print(KeyInfo:GetUserIds())
print(KeyInfo:GetMetadata())
else
print(CurrentNumber)
end
end
local function LoadPlayerLevelData(Player)
local PlayerPointsKey = Player.UserId
local Sucess, CurrentNumber, KeyInfo:DataStoreKeyInfo = pcall(function()
return PointsData:GetAsync(PlayerPointsKey, Player:WaitForChild("leaderstats").Level.Value)
end)
if Sucess then
print("Level:"..CurrentNumber)
print("Level:"..KeyInfo.Version)
print("Level"..KeyInfo.CreatedTime)
print("Level"..KeyInfo.UpdatedTime)
print(KeyInfo:GetUserIds())
print(KeyInfo:GetMetadata())
else
print(CurrentNumber)
end
end
Players.PlayerRemoving:Connect(SaveStatsData)
Players.PlayerAdded:Connect(LoadPlayerPointsData)
Players.PlayerAdded:Connect(LoadPlayerLevelData)
Output:
Points: 0 - Server - script:39
Points: (Version) - Server - script:40
Points: (Created Time) - Server - script:41
Points: (Updated Time) - Server - script:42
etc.
Data Saved - Server
â It says that the data store saved.