You can write your topic however you want, but you need to answer these questions:
- What do you want to achieve?
I am making a save system with datastore. It should save a leaderstat value.
-
What is the issue?
The issue is that that it doesn’t always save. There’s around a 70% success rate. I am not sure how to send an image or video describing the situation, but when leaving and rejoining on Studio, there’s a chance it won’t save. The error is on Player too. -
What solutions have you tried so far?
I have tried looking into a couple devforum posts, however they had different issues from me. They had a problem of saving too many different objects and variables at the same time, whereas I only have one value in my code that is breaking.
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
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.Parent = leaderstats
local Data
local success, errormessage = pcall(function()
Data = myDataStore:GetAsync(Player.UserId, Player.leaderstats.Points)
end)
if success then
Points.Value = Data
elseif errormessage then
print("failed to find data")
warn(errormessage)
end
end)
Players.PlayerRemoving:Connect(function(Player)
local success, errormessage = pcall(function()
myDataStore:SetAsync(Player.UserId,Player.leaderstats.Points.Value)
end)
if success then
print("successfully saved!")
elseif errormessage then
print("not successful")
warn(errormessage)
end
end)
Thank you for your help!
(Note- when the value saves, it should print “successfully saved!”)