Hello! I'm pretty new to datastores and I'm currently having a problem where the initial value (1) created in a new player seems to be overwritten with 0 even though there was no data to begin with. There's probably an obvious solution to this, but I can't figure out why this is happening. There are no errors.
local DataStoreService = game:GetService("DataStoreService")
local SkipData = DataStoreService:GetDataStore("Skips")
game.Players.PlayerAdded:Connect(function(player)
local Skips = Instance.new("IntValue")
Skips.Name = "Skips"
Skips.Parent = player
Skips.Value = 1 -- initial value
local data
local success, errormessage = pcall(function()
data = SkipData:GetAsync(player.UserId.."-Skips")
end)
if success then
Skips.Value = data
else
print("There was an error whilst getting your data")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
SkipData:SetAsync(player.UserId.."-Skips", player.Skips.Value)
end)
if success then
print("Player Data successfully saved!")
else
print("There was an error when saving data")
warn(errormessage)
end
end)
game:BindToClose(function()
if not game:GetService("RunService"):IsStudio() then
for i, player in pairs(game.Players:GetPlayers()) do
local success, errormessage = pcall(function()
SkipData:SetAsync(player.UserId.."-Skips", player.Skips.Value)
end)
if not success then
warn(errormessage)
end
end
end
end)
Thanks in advance!

5 comments
You could try printing the leaderstat value and the data given from the datastore and see the difference to check if you set the value correctly. You code looks ok other than that.
You could do: Skips.Value = SkipData:GetAsync(player.UserId.."-Skips") or 1
Plus pcall it ^
This seems to work! Thanks!
No problem!