Hello all,
This is my first time working with data stores but for some reason every thing I've tried hasn't worked.
I've watched at least 3 tutorials, following closely each time and every time the data store fails to work/load data in.
On Studio I have API Services enabled, so I don't believe that's the problem.
Here is the Script
, located in ServerScriptService
local Data = game:GetService("DataStoreService"):GetDataStore("PantherPoints") game.Players.PlayerAdded:Connect(function(plr) local folder = Instance.new("Folder") folder.Name = "leaderstats" folder.Parent = plr local cash = Instance.new("IntValue",folder) cash.Name = "Panther Points" local saveditems = Data:GetAsync(plr.UserId) if saveditems then cash.Value = saveditems:WaitForChild("Panther Points").value else cash.Value = 0 end end) game.Players.PlayerRemoving:Connect(function(plr) local saved = {["Panther Points"] = plr:WaitForChild("leaderstats"):WaitForChild("Panther Points").Value} Data:SetAsync(plr.UserId,saved) end)
Any help would be greatly appreciated.
Hey, I know a lot of people have this problem where there's nothing wrong with their script, but the server fails to save their data and reload it when they rejoin. This happens when the server shuts down before the last player leaves, thus not running your PlayerRemoving
function which is supposed to save your data. This could also happen when you're playing solo in the Studio. To fix this problem, use :BindToClose
. :BindToClose
will allow all your remaining functions to run before the server shuts down. Do not delete anything from your original script. Simply copy and paste your PlayerRemoving
function (take out PlayerRemoving though and use a loop to loop through all the players in the server) into the :BindToClose
function at the end.
local Data = game:GetService("DataStoreService"):GetDataStore("PantherPoints") game.Players.PlayerAdded:Connect(function(plr) local folder = Instance.new("Folder") folder.Name = "leaderstats" folder.Parent = plr local cash = Instance.new("IntValue",folder) cash.Name = "Panther Points" local saveditems = Data:GetAsync(plr.UserId) if saveditems then cash.Value = saveditems:WaitForChild("Panther Points").Value -- capitalize the v in Value as well else cash.Value = 0 end end) game.Players.PlayerRemoving:Connect(function(plr) local saved = {["Panther Points"] = plr:WaitForChild("leaderstats"):WaitForChild("Panther Points").Value} Data:SetAsync(plr.UserId,saved) end) game:BindToClose(function() for _, plr in pairs(game.Players:GetPlayers()) do local saved = {["Panther Points"] = plr:WaitForChild("leaderstats"):WaitForChild("Panther Points").Value} Data:SetAsync(plr.UserId,saved) end end)