I want to save 2 int values, the first one is saving, everything is fine but when i try to save the second one is not saving at all.
I looked on google for a fix, i modified the whole script as i found online and is still not working
Here is the code:
local DataStoreService = game:GetService("DataStoreService") local KingPointsStore = DataStoreService:GetDataStore("KingPointsStore") local tutorialStore = DataStoreService:GetDataStore('tutorialStore') game.Players.PlayerAdded:Connect(function(player) local leaderstats = Instance.new("Folder") leaderstats.Name = "Stats" leaderstats.Parent = player local King = Instance.new("IntValue") King.Name = "KingPoints" King.Parent = leaderstats local tutorial = Instance.new('IntValue') tutorial.Name = 'Tutorial' tutorial.Parent = leaderstats tutorial.Value = 0 local UserId = player.UserId local kingData local tutorialData local success, errormessage = pcall(function() kingData = KingPointsStore:GetAsync(UserId) tutorialData = tutorialStore:GetAsync(UserId) end) if success then King.Value = kingData tutorial.Value = tutorialData end end) game.Players.PlayerRemoving:Connect(function(player) local UserId = player.UserId local kingData = player.Stats.KingPoints.Value local tutorialData = player.Stats.Tutorial.Value KingPointsStore:SetAsync(UserId, kingData) tutorialStore:SetAsync(UserId, tutorialData) print('Data saved') end)
Hello! Try this code instead:
local DSS = game:GetService("DataStoreService") local Store = DSS:GetDataStore("StoreValues") game.Players.PlayerAdded:Connect(function(plr) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = plr local King = Instance.new("IntValue") King.Name = "KingPoints" King.Parent = leaderstats local Tutorial = Instance.new("IntValue") Tutorial.Name = 'Tutorial' Tutorial.Parent = leaderstats local data local success,err = pcall(function() data = Store:GetAsync(plr.UserId) end) if not success then error("Could not get player data.") else if data ~= nil then King.Value = data[1] Tutorial.Value = data[2] else warn("No data found.") end end end) game.Players.PlayerRemoving:Connect(function(plr) local data = {} data[1] = plr.leaderstats.KingPoints.Value data[2] = plr.leaderstats.Tutorial.Value Store:SetAsync(plr.UserId,data) end)
If you want an explanation, you can ask and you shall recieve.