--//variables local DataStoreService = game:GetService("DataStoreService") local LevelStore = DataStoreService:GetDataStore("Levelllssss") --//player added game.Players.PlayerAdded:Connect(function(player) --//creating level local LevelFolder = Instance.new("Folder",player) LevelFolder.Name = "LevelStore" local Level = Instance.new("IntValue",LevelFolder) Level.Name = "Level" Level.Value = LevelStore:GetAsync(player.UserId,Level.Value)or 1 --//creating needed xp local NeededXP = Instance.new("IntValue",LevelFolder) NeededXP.Name = "NeededXP" NeededXP.Value = LevelStore:GetAsync(player.UserId,NeededXP.Value)or 1000 --//creating current xp local XPP = Instance.new("IntValue",LevelFolder) XPP.Name = "XP" XPP.Value = LevelStore:GetAsync(player.UserId,XPP.Value)or 0 --//main handling local success,level = pcall(function() return LevelStore:GetAsync(player.UserId,Level.Value,NeededXP.Value,XPP.Value) end) if success then LevelStore:SetAsync(player.UserId,Level.Value,NeededXP.Value,XPP.Value) else player:Kick("Data Error | Please rejoin!") end end) --//Save when player leaves game.Players.PlayerRemoving:Connect(function(player) LevelStore:SetAsync(player.UserId,player.LevelStore.Level.Value,player.LevelStore.NeededXP.Value,player.LevelStore.XP.Value) end)
So I started learning how to do datastores yesterday. Today I ran into a problem when trying to create my level system. When I first join the game, the values get set correctly. When I join the game for the second time to see if it all saved, it saves at one? Why is this? I'm new to datastores and kind of need help. I can't spot where it's all saving as 1.
If you want to save multiple values to the same datastore, you will need to wrap them in a table. I have modified your code to show how this could be done:
--//variables local DataStoreService = game:GetService("DataStoreService") local LevelStore = DataStoreService:GetDataStore("Levelllssss") --//player added game.Players.PlayerAdded:Connect(function(player) -- Get the levelstore data into a variable (levelInfo) local success, levelInfo = pcall(function() return LevelStore:GetAsync(player.UserId) end) if success then -- If there was no data, then setup levelInfo with default values if not levelInfo then levelInfo = { Level = 1, NeededXP = 1000, XPP = 0 } end else -- Error retrieving data from datastore player:Kick("Data Error | Please rejoin!") end --//creating level folder -- Avoid using the parent option of Instance.new, as it can cause lag. Prefer to setup your object and then parent it manually local LevelFolder = Instance.new("Folder") LevelFolder.Name = "LevelStore" LevelFolder.Parent = player --//Creating level local Level = Instance.new("IntValue", LevelFolder) Level.Name = "Level" Level.Value = levelInfo.Level --//creating needed xp local NeededXP = Instance.new("IntValue", LevelFolder) NeededXP.Name = "NeededXP" NeededXP.Value = levelInfo.NeededXP --//creating current xp local XPP = Instance.new("IntValue", LevelFolder) XPP.Name = "XP" XPP.Value = levelInfo.XPP -- No need to save data here. If the data was nil then you're just saving default values, which is not needed, -- and if it wasn't nil, you're just saving the same info the datastore already contained back into it end) --//Save when player leaves game.Players.PlayerRemoving:Connect(function(player) local levelInfo = { Level = player.LevelStore.Level.Value, NeededXP = player.LevelStore.NeededXP.Value, XPP = player.LevelStore.XP.Value } local success = pcall(function() LevelStore:SetAsync(player.UserId, levelInfo) end) -- Need to deal with data failing to save end)
Note that if you are testing this in play solo in studio, the game will exit before anything gets saved. If you create a local server in studio or try it in a published game then everything will work correctly. Also, I recommend following the tutorial on this page for your data saving code. This will give you much more robust error handling.