What do i want to achieve? I’m making a datastore for my game to save IntValues and Tools.
So before i tried saving tools it worked fine, but now that i’ve added a save for tools
I buy the tool, and then leave the game, i join back the tool was saved and is in the backpack, but my gold and level is reset
Also i keep getting this warning about the data store queue
local store = game:GetService("DataStoreService")
local upg = store:GetDataStore('v0.1111111')
function OnAdded(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local secondaryData = Instance.new("Folder",player)
secondaryData.Name = "secondaryData"
local xp = Instance.new("IntValue",secondaryData)
xp.Name = "Xp"
local xp_max = Instance.new("IntValue",secondaryData)
xp_max.Name = "Xp_Max"
xp_max.Value = 100
local Gold = Instance.new("IntValue",leaderstats)
Gold.Name = "Gold"
Gold.Value = 1000
local level = Instance.new("IntValue",leaderstats)
level.Name = "Level"
level.Value = 1
local success,err = pcall(function()
if upg then
local data = upg:GetAsync(player.UserId)
if data then
--getting the values from the table
xp.Value = data.Xp
xp_max.Value = data.Xp_Max
Gold.Value = data.Gold
level.Value = data.Level
for _,toolName in pairs(data) do
local tool = game.ServerStorage.Items.Tools:FindFirstChild(toolName)
if tool then
local newTool = tool:Clone()
newTool.Parent = player.Backpack
local newTool = tool:Clone()
newTool.Parent = player.StarterGear
end
end
end
end
end)
if success then
warn(player.Name..' Stats have loaded')
elseif err then
warn(err,'error has ocurred')
end
end
function OnRemove(player)
local stats = player:WaitForChild("leaderstats")
local stats2 = player:WaitForChild("secondaryData")
local Gold = stats.Gold
local Xp = stats2.Xp
local Xp_Max = stats2.Xp_Max
local Level = stats.Level
local save = {
['Gold'] = Gold.Value;
['Xp'] = Xp.Value;
['Xp_Max'] = Xp_Max.Value;
['Level'] = Level.Value;
}
for _,tool in pairs(player.Backpack:GetChildren()) do
if game.ServerStorage.Items.Tools:FindFirstChild(tool.Name) then
table.insert(save,tool.Name)
end
end
local success,err = pcall(function()
upg:SetAsync(player.UserId,save)
end)
if success then
warn(player.Name..' Stats have been saved')
elseif err then
warn(err)
end
end
game.Players.PlayerAdded:Connect(OnAdded)
game.Players.PlayerRemoving:Connect(OnRemove)
game:BindToClose(function()
for i,v in pairs(game.Players:GetPlayers()) do
OnRemove(v)
end
end)