Ok, this has to be a simple fix, but I have been searching around for about an hour looking for something or someone to help me out.
What have you tried so far?
I have asked for help in two different discord servers. Only got a few people responding, and none of there solutions working.
What is the issue?
Here is my script.
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local XP = Instance.new("IntValue")
XP.Name = "XP"
XP.Parent = leaderstats
local Level = Instance.new("IntValue")
Level.Name = "Level"
Level.Parent = leaderstats
local playerUserId = "Player_"..player.UserId
--Local Data
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(playerUserId)
end)
if success then
XP.Value = data[1]
Level.Value = data[2]
--Set the data equal to the current XP
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = "Player_"..player.UserId
local data = {
XP = player.leaderstats.XP.Value;
Level = player.leaderstats.Level.Value;
}
local success, errormessage = pcall(function()
myDataStore:SetAsync(playerUserId, data)
end)
if success then
print("Data saved")
else
print("Error in saving data")
warn(errormessage)
end
end)
I was getting an error but now I am not, I haven’t changed my script. I am so confused.
“myDataStore” seems like a common datastore name, maybe you have used it before and data you are reading is not what you expected?
Also, you have showed your code but still have not explained the problem. If you don’t know what the problem is, maybe you could print debug to see where your code stops working.
Hi, I think it’s worth checking whether the initial GetAsync is nil (from someone who has never saved), then you can set both leaderstats to 0 and SetAsync() it. Also, add some prints after each pcall success and see the output to see if your code is even running.
Personally, I would strongly recommend against learning DataStore2 (a DataStoreService wrapper) before learning DataStoreService. It’s not practical to forgo or delay the gaining of knowledge of a crucial API in it’s normal form.
I’m not saying it doesn’t work, I’m saying that the focus should be on learning DataStoreService first. Also IncrementAsync() exists on DSS as well as DS2 (DataStore2:Increment())