My data store isn’t saving data when a player leaves and rejoins the game.
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local points = Instance.new("IntValue")
points.Name = "Points"
points.Value = 0
points.Parent = leaderstats
local wins = Instance.new("IntValue")
wins.Name = "Wins"
wins.Value = 0
wins.Parent = leaderstats
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(player.Userid.."-wins")
end)
if success then
wins.Value = data
else
print("There was an erro whilst getting your data")
warn(errormessage)
end
end)
Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
myDataStore:SetAsync(player.UserId.."-wins",player.leaderstats.Wins.Value)
end)
if success then
print("Player Data Successfully Saved")
else
print("There was an error when saving data")
warn(errormessage)
end
end)
There doesn’t seem to be anything wrong with the script, so it is likely a problem with something else. Is all of this code running? Did you check the console to see if it saved correctly?
If your only testing this in studio, it usually wont work because the server is instantly shutdown and there isn’t enough time to finish saving data. To fix this you can add a wait(2) in the game.BindToClose event so it has two seconds to save.
@YummyLava57 This didn’t work, at least not how I implemented it. Is there anything wrong with how I implemented it?
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
local Players = game:GetService("Players")
game:BindToClose(function()
wait(2)
end)
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local points = Instance.new("IntValue")
points.Name = "Points"
points.Value = 0
points.Parent = leaderstats
local wins = Instance.new("IntValue")
wins.Name = "Wins"
wins.Value = 0
wins.Parent = leaderstats
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(player.Userid.."-wins")
end)
if success then
wins.Value = data
else
print("There was an erro whilst getting your data")
warn(errormessage)
end
end)
Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
myDataStore:SetAsync(player.UserId.."-wins",player.leaderstats.Wins.Value)
end)
if success then
print("Player Data Successfully Saved")
else
print("There was an error when saving data")
warn(errormessage)
end
end)
Okay so it didn’t work because you need to have 2 separate scripts for each datastore, you can’t put them all in one. Also if you haven’t already make sure they’re in workspace and lmk if it works