So i have a datastore script,and there are three stats : cash,level and exp.So when i press play and earnt a few levels,some cash and exp and stop and play again,the exp resets to 0 and the cash and level resets to the same value.How does this keep happening?Also i realised that whenever it resets,the cash value and the level value is the same However,it works fine on other games i used this script in and there were no resets script:
local datastore = game:GetService("DataStoreService") local ds1 = datastore:GetDataStore("cashSaveSystem") local ds2 = datastore:GetDataStore("killsavesystem") local ds3 = datastore:GetDataStore("levelupsave") game.Players.PlayerAdded:connect(function(plr) local stats = Instance.new ("IntValue", plr) stats.Name = "leaderstats" local Experience = Instance.new ("IntValue",stats) Experience.Name = "EXP" local level = Instance.new ("IntValue",stats) level.Name = "Level" local cash = Instance.new("IntValue",stats) cash.Name = "Cash" Experience.Value = ds1:GetAsync(plr.UserId) or 0 ds1:SetAsync(plr.UserId, Experience.Value) cash.Value = ds2:GetAsync(plr.UserId) or 0 ds2:SetAsync(plr.UserId, cash.Value) level.Value = ds3:GetAsync(plr.UserId) or 0 ds2:SetAsync(plr.UserId,level.Value) Experience.Changed:Connect(function() ds1:SetAsync(plr.UserId, Experience.Value) end) cash.Changed:Connect(function() ds2:SetAsync(plr.UserId, cash.Value) end) level.Changed:Connect(function() ds3:SetAsync(plr.UserId,level.Value) end) Experience.Changed:connect(function() level.Value = math.floor(Experience.Value / 1000) end) end)
Level up script
local player = game.Players.LocalPlayer local Leaderstats = player:WaitForChild('leaderstats') local level = Leaderstats:WaitForChild ('Level') local xp = Leaderstats:WaitForChild('EXP') level.changed:connect(function() if xp.Value < 1000 then level.Value = 0 elseif xp.Value >= 1000 then level.Value = 1 xp.Value = 0 end end)
If you try to do datastores in studio
it doesnt save
unless your playing ingame and have API service on it wont work
If you'd went through all of the code, you'll realize that you save the values of the player upon joining, not when they're leaving.
It's best to make a function that saves players' data, with an error handler.
Try this in your code.
function save(player) local success,message = pcall(function() ds1:SetAsync(player.UserId, player.leaderstats.Experience.Value) ds2:SetAsync(player.UserId,player.leaderstats.Cash.Value) ds3:SetAsync(player.UserId,player.leaderstats.Level.Value) end) if success then print("Successfully saved "..player.Name.."'s data") else print("Failed to save "..player.Name.."'s data") end end game.Players.PlayerRemoving:Connect(function(player) save(player) end)
I don't know what is wrong with cash but I see a problem with level and xp
level.changed:connect(function() if xp.Value < 1000 then level.Value = 0 elseif xp.Value >= 1000 then level.Value = 1 xp.Value = 0 end end)
what you have here runs when a players level changes it then checks if their xp is less than 1000 and if it is sets their level to 0 which gets called over and over because it keeps changing the value of level but if the xp is more than or equal to 1000 then it sets their level to 1 and their xp to zero since you change their level it runs again but you have set their xp to 0 so it sets their level instantly back to 0
if you want a player to level up every 1000 xp then do this
xp.changed:connect(function() if xp.Value >= 1000 then level.Value = level.Value+1 xp.Value = 0 end end)
then remove this from the first script
Experience.Changed:connect(function() level.Value = math.floor(Experience.Value / 1000) end)