Hi, recently, I was trying to make a datastore for player points, the issue here is that when the player leaves, PlayerRemoving event works but it doesn’t save. I searched for solutions but I didn’t find/understand. I tried to determine where exactly where the problem then I found out that it is in that part (PlayerRemoving event).
local dataStoreService = game:GetService("DataStoreService")
local myDataStore = dataStoreService:GetDataStore("playersPointSStorage")
game.Players.PlayerAdded:Connect(function(player)
local leaderstatefolder = Instance.new("Folder")
leaderstatefolder.Name = "leaderstats"
leaderstatefolder.Parent = player
local Points = Instance.new("IntValue")
Points.Name = "Points"
Points.Parent = leaderstatefolder
local playerId = player.UserId
--load Data
local data
local success, errorMassage = pcall(function()
data = myDataStore:GetAsync(playerId)
end)
if success then
Points.Value = data to data (Our DataStore)
end
end)
--------------------------------------------------------------------------------
game.Players.PlayerRemoving:Connect(function(player)
local playerId = player.UserId
local data = player.leaderstats.Points.Value
local success, errorMassage = pcall(function()
myDataStore:SetAsync(playerId, data)
end)
if success then
print("The points has saved")
else
print("The points hasn't saved")
warn(errorMassage)
end
end)
Sorry if I missed something stupid, I’m a beginner.
Note: The points were added by clicking a part in the studio.