I have a DataStore that saves Points and Wins, however I have a weird problem with it.
As seen down below, the DataStore will print out “Player Data saved successfully” when it works. It does that each time I leave the game. But the data does not actually save, the Points value stays at 0 when I rejoin the game.
I have BindToClose() working, so I do not know why this isn’t saving my data.
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore")
game.Players.PlayerAdded:Connect(function(plr)
local ls = Instance.new("Folder", plr)
ls.Name = "leaderstats"
local Wins = Instance.new("IntValue", ls)
Wins.Name = "Survivals"
local Cash = Instance.new("IntValue", ls)
Cash.Name = "Points"
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(plr.UserId.."-Cash")
end)
if success then
Cash.Value = data or 0
print(Cash.Value)
else
print("There was an error getting your data")
warn(errormessage)
end
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(plr.UserId.."-Wins")
end)
if success then
Wins.Value = data or 0
print(Wins.Value)
else
print("There was an error getting your data")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
myDataStore:SetAsync(player.UserId.."-Cash",player.leaderstats.Points.Value)
print(player.leaderstats.Points.Value)
myDataStore:SetAsync(player.UserId.."-Wins",player.leaderstats.Survivals.Value)
print(player.leaderstats.Survivals.Value)
end)
if success then
print("Player Data saved successfully")
print(player.leaderstats.Points.Value)
else
print("there was an error when saving data")
warn(errormessage)
end
end)
game:BindToClose(function()
wait(5)
end)