Datastore Error

Hello, this datastore is not saving. Any ideas?

local DataStoreService = game:GetService("DataStoreService")

local TotalNotesSave = DataStoreService:GetDataStore(“Notes”)

game.Players.PlayerAdded:Connect(function(player)
local Folder = Instance.new(“Folder”)
Folder.Parent = player
Folder.Name = “NotesFolder”

local Value = Instance.new("NumberValue")
Value.Parent = Folder
Value.Name = "Notes"

TotalNotesSave:GetAsync(player.UserId , Value.Value)

Value.Changed:Connect(function()
	TotalNotesSave:SetAsync(player.UserId , Value.Value)
end)

game.Players.PlayerRemoving:Connect(function(player)
	TotalNotesSave:SetAsync(player.UserId , Value.Value)
end)

end)

Just in-case, have you done your research before posting here?

1 Like

No, I actually did not. Should I?

Here, let me tgry to figure this out then.

1 Like

In the future make sure ALL your code is put into your code block format. The first 5 lines aren’t and the fact that the error lies there makes it harder to notice.

The issue is that your Players.PlayerRemoving event is nested in your Players.PlayerAdded event. Move the event outside of the PlayerAdded event and replace the function with this:

game.Players.PlayerRemoving:Connect(function(player)
	local Value = player.NotesFolder.Notes.Value
	TotalNotesSave:SetAsync(player.UserId , Value)
end)

This code isn’t sustainable in the long term in some cases because the player instance might disappear before the script can get their notes value. I’d suggest having another folder in ServerStorage where you get your values from when the player leaves.

1 Like

Oh, okay! Thanks so much for the help!