So I was working on a rank system for my game, and I wanted to use data stores to save data and load, but I found a problem. Data never got saved, and no errors ever came up, I don’t know why, but It’s not working. I also have API requests enabled.
Code:
local DSS = game:GetService("DataStoreService")
local RankDS = DSS:GetDataStore("RankDataStore")
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local levelValue = Instance.new("IntValue")
levelValue.Name = "Rank"
levelValue.Parent = leaderstats
local expValue = Instance.new("NumberValue")
expValue.Name = "expValue"
expValue.Parent = plr
local maxExp = Instance.new("NumberValue")
maxExp.Name = "maxExp"
maxExp.Parent = plr
local function LevelUp()
levelValue.Value += 1
expValue.Value = 0
maxExp.Value = 1000
end
local function GainExp(gain)
expValue.Value += gain
if expValue.Value > maxExp.Value then
LevelUp()
end
end
while wait(1) do
GainExp(100)
end
--Load data
local PlrId = "Player_"..plr.UserId
print("First ID var has been set.")
local data
local success, errormessage = pcall(function()
data = RankDS:GetAsync(PlrId)
end)
print("First pcall has been fired.")
if success then
print("Success = true!")
levelValue.Value = data
end
end)
--Save data
game.Players.PlayerRemoving:Connect(function(plr)
local PlrId = "Player_"..plr.UserId
local data = plr.leaderstats.Rank.Value
print("Second variables has been set!")
local success, errormessage = pcall(function()
RankDS:UpdateAsync(PlrId,data)
end)
print("Second pcall has been fired!")
if success then
print("Data has been saved successfully")
else
print("There has been an error")
warn(errormessage)
end
end)
Maybe you’re using UpdateAsync() wrong like @AxeI_c said. It seems like you are doing SetAsync() but UpdateAsync() works differently. You can do it how @AxeI_c said or you can try using SetAsync().
That fixed it, kind of. My data did save and load yes, but even tho I set my rank to 4, it keeps setting my rank to 2 not 4, even though I set my rank to 4 on the server.