I am new to working with data storage and I can get one data store working by itself, for example having a "coins" stat for all players that can save and it runs perfectly. However now I am trying to add a second data score for player xp and I am having some issues. There are no reported errors and I can't find any grammatical errors so I think I may be doing this incorrectly. The leaderstats work and the XP shows up for the player, but it will not save. The "bucks" value still saves, so I am at a loss. Any help would be appreciated.
local bucksDataStores = game:GetService("DataStoreService"):GetDataStore("BucksDataStore")
local xpDataStores = game:GetService("DataStoreService"):GetDataStore("XPDataStore")
local defaultBucks = 15
local defaultXP = 0
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local bucks = Instance.new("IntValue")
bucks.Name = "Bucks"
bucks.Value = defaultBucks
bucks.Parent = leaderstats
local xp = Instance.new("IntValue")
xp.Name = "XP"
xp.Value = defaultXP
xp.Parent = leaderstats
-- Datastores
local playerBucksData
local playerXPData
pcall(function()
playerBucksData = bucksDataStores:GetAsync(player.UserId.."-Bucks") --example: 12323425-Bucks
playerXPData = xpDataStores:GetAsync(player.UserId.."XP")
end)
if playerBucksData ~= nil then
-- Player has saved data, load it in
bucks.Value = playerBucksData
else
--New player
bucks.Value = defaultBucks
end
if playerXPData ~= nil then
-- Player has saved data, load it in
xp.Value = playerXPData
else
--New player
xp.Value = defaultXP
end
end)
local bindableEvent = Instance.new("BindableEvent")
game.Players.PlayerRemoving:Connect(function(player)
pcall(function()
bucksDataStores:SetAsync(player.UserId.."-Bucks",player.leaderstats.Bucks.Value)
print("Bucks Data saved")
xpDataStores:SetAsync(player.UserId.."-XP",player.leaderstats.XP.Value)
print("XP data saved")
bindableEvent:Fire()
end)
end)

1 comment
o.o