I’m testing my Data Store Script that stores 2 values, Level data and XP data. It saves the data in studio, I can tell because when I look at my data with the “DataStore Editor” plugin it shows that I have data, but if I test the script in an actual game, like actually playing the game, I always spawn in with 0 Level Data and I do edit my Level Data in the console to test it, its just that when I leave and join back I have 0 Level data instead of whatever I put. Right now I am only testing my Level Data, not XP.
local dataservice = game:GetService("DataStoreService")
local LevelData = dataservice:GetDataStore("PlayerLevels")
local function savedata(plyr)
local id = "Player_"..plyr.UserId
local Data = {
level = plyr.leaderstats.Level.Value,
XP = plyr.XP.Value
}
local success, errormessage = pcall(function()
LevelData:SetAsync(id, Data)
end)
if success then
print("dayta saved")
else
print("error saving dayta")
warn(errormessage)
end
end
game.Players.PlayerAdded:Connect(function(plyr)
wait(0.1)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
local XP = Instance.new("IntValue")
XP.Name = "XP"
local level = Instance.new("IntValue")
level.Name = "Level"
level.Parent = leaderstats
local id = "Player_"..plyr.UserId
local Data
local success, errormessage = pcall(function()
Data = LevelData:GetAsync(id)
end)
if success then
if Data then
level.Level.Value = Data.Level
XP.Value = Data.XP
leaderstats.Parent = plyr
XP.Parent = plyr
end
end
spawn(function()
while true do
wait(60)
print("Saving")
savedata(plyr)
end
end)
end)
game.Players.PlayerRemoving:Connect(function(plyr)
savedata(plyr)
end)
game:BindToClose(function()
for i, plyr in pairs(game.Players:GetPlayers()) do
savedata(plyr)
end
end)
----NEW----
Clarification
Every time i go to test my script in an actual game, it doesnt load my data. Though, if i test it in the studio, The Data Store plugin I use to see data storages shows that it saved the data, but it doesnt load it when i go back into the test mode on studio, im pretty sure this is also happening on the actual game version.
local dataservice = game:GetService("DataStoreService")
local LevelData = dataservice:GetDataStore("PlayerLevels")
local function savedata(plyr)
local id = "Player_"..plyr.UserId
local Data = {
Level = plyr.leaderstats.Level.Value,
XP = plyr.XP.Value
}
local success, errormessage = pcall(function()
LevelData:SetAsync(id, Data)
end)
if success then
print("dayta saved")
else
print("error saving dayta")
warn(errormessage)
end
end
game.Players.PlayerAdded:Connect(function(plyr)
wait(0.25)
local XP = Instance.new("IntValue")
XP.Name = "XP"
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plyr
XP.Parent = plyr
local IntLevel = Instance.new("IntValue")
IntLevel.Name = "Level"
IntLevel.Parent = leaderstats
local id = "Player_"..plyr.UserId
local Data
local success, errormessage = pcall(function()
Data = LevelData:GetAsync(id)
end)
if success then
if Data then
IntLevel.Value = Data.Level
XP.Value = Data.XP
end
end
spawn(function()
while true do
wait(60)
print("Saving")
savedata(plyr)
end
end)
end)
game.Players.PlayerRemoving:Connect(function(plyr)
savedata(plyr)
end)
game:BindToClose(function()
for i, plyr in pairs(game.Players:GetPlayers()) do
savedata(plyr)
end
end)