This is my script for creating and saving the 'Skips' value inside the player:
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("SkipsDataStore")
game.Players.PlayerAdded:Connect(function(player)
local SkipStages = Instance.new("Folder", player)
SkipStages.Name = "SkipStages"
local Skips = Instance.new("IntValue", SkipStages)
Skips.Name = "Skips"
Skips.Value = 3
local data
local success, errormessage = pcall(function()
data = DataStore:GetAsync(player.UserId)
end)
if success then
Skips.Value = data.Skips
else
print("Error while fetching data!")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
local data = {
Skips = player.SkipStages.Skips.Value
}
DataStore:SetAsync(player.UserId, data)
end)
if success then
print("Skips Successfully Saved!")
else
print("There was an error while saving Skips!")
warn(errormessage)
end
end)
Currently, I have named my DataStore as "SkipsDataStore". If I change that name to something else, then my data does not save and the output gives me the error, "* ServerScriptService.Skips:19: attempt to index nil with 'Skips'*"
But the above error is ONLY when I change the name to something other than "SkipsDataStore".
Why is that? I have been using DataStores before also and I was able to change their name without any errors.
So why is this happening?
Thanks for any help

2 comments
Thank you! Just one question regarding bindtoclose function. If I am implementing this in my code, then should I remove the player removing function? doesn't bindtoclose function take care of that? If so, then where you wrote ' you code of saving data' should I add what I wrote in player removing event?
DO NOT REMOVE PLAYER REMOVING. As BindToClose will only trigger when the server is about to close. And ya, where I wrote 'your code of saving data', you can add the code in PlayerRemoving. If you using identical variables as my code, change the plr variabe in BindToClose to player.