Sometimes it saves to the storage but when i join it doesnt load. anyone know?
local dataStoreService = game:GetService("DataStoreService") local dataStore = dataStoreService:GetDataStore("LevelStorage") game.Players.PlayerAdded:Connect(function(plr) local leaderstats = Instance.new("Folder", plr) leaderstats.Name = "leaderstats" local cash = Instance.new("NumberValue", leaderstats) cash.Name = "Cash" cash.Value = 0 local OwnsTycoon = Instance.new("BoolValue", plr) OwnsTycoon.Name = "OwnsTycoon" OwnsTycoon.Value = false local level = Instance.new("IntValue",leaderstats) level.Name = "Level" level.Value = 1 local Leveldata local success, errormessage = pcall(function() Leveldata = dataStore:GetAsync(plr.UserId.."level", level.Value) end) if success then cash.Value = Leveldata print("Data Loaded from storage") else print("Error while saving data") print(errormessage) end end) game.Players.PlayerRemoving:Connect(function(plr) local success, errormessage = pcall(function() dataStore:SetAsync(plr.UserId.."level",plr.leaderstats.Level.Value) end) if success then print("Player Data saved") else print("Something went wrong") warn(errormessage) end end)
Usually when I write DataStores I use
Leveldata = dataStore:GetAsync(plr.UserId)
instead of
Leveldata = dataStore:GetAsync(plr.UserId.."level", level.Value)
for GetAsync().
I'm not entirely sure on how this would change much but it's worth a try.
New code:
local dataStoreService = game:GetService("DataStoreService") local dataStore = dataStoreService:GetDataStore("LevelStorage") game.Players.PlayerAdded:Connect(function(plr) local leaderstats = Instance.new("Folder", plr) leaderstats.Name = "leaderstats" local cash = Instance.new("NumberValue", leaderstats) cash.Name = "Cash" cash.Value = 0 local OwnsTycoon = Instance.new("BoolValue", plr) OwnsTycoon.Name = "OwnsTycoon" OwnsTycoon.Value = false local level = Instance.new("IntValue",leaderstats) level.Name = "Level" level.Value = 1 local Leveldata local success, errormessage = pcall(function() Leveldata = dataStore:GetAsync(plr.UserId) end) if success then cash.Value = Leveldata print("Data Loaded from storage") else print("Error while saving data") print(errormessage) end end) game.Players.PlayerRemoving:Connect(function(plr) local success, errormessage = pcall(function() dataStore:SetAsync(plr.UserId.."level",plr.leaderstats.Level.Value) end) if success then print("Player Data saved") else print("Something went wrong") warn(errormessage) end end)