DataStore not saving value?

Oddly enough, the value isn’t being saved. Can anyone help?

local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData4")

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local function onPlayerJoin(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local night = Instance.new("NumberValue")
	night.Name = "Night"
	night.Parent = leaderstats

	local cheese = Instance.new("BoolValue")
	cheese.Name = "Cheese"
	cheese.Parent = leaderstats
	
	local first = Instance.new("BoolValue")
	first.Name = "firstPlay"
	first.Parent = leaderstats

	local playerUserId = "Player_" .. player.UserId


	local data = playerData:GetAsync(playerUserId)
	if data then
		night.Value = data
		cheese.Value = data
		first.Value = data

		if night.Value == 0 then
			night.Value = 1
		end
	elseif not data then
		night.Value = 1
		cheese.Value = false
		first.Value = true
	end
end



local function onPlayerExit(player)
	local success, err = pcall(function()
		local playerUserId = "Player_" .. player.UserId
		playerData:SetAsync(playerUserId, player.leaderstats.Night.Value)
		playerData:SetAsync(playerUserId, player.leaderstats.Cheese.Value)
	end)

	if not success then
		warn('Could not save data!')
	end
end


game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)

ReplicatedStorage.First.OnServerEvent:Connect(function(Player)
	Player["leaderstats"].firstPlay.Value = false
end)
game.ReplicatedStorage["First"]:FireServer() -- client

The firstPlay value isn’t been saved.

1 Like

You are not even saving your firstPlay value in the onPlayerExit function, you are just saving Night and Cheese. Second your only saving Data under 1 key, you will need to create multiple keys for each DataStore or Save the data under 1 key in a dictionary and load it in a dictionary

2 Likes
playerData:SetAsync(playerUserId, player.leaderstats.Night.Value)
playerData:SetAsync(playerUserId, player.leaderstats.Cheese.Value)

The previous reply is correct, in addition to what they stated, “Cheese’s” value would end up overriding “Night’s” value in the DataStore.

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("dataNightStore")

local replicatedStorage = game:GetService("ReplicatedStorage")

game.Players.PlayerAdded:Connect(function(player)

	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"

	local night = Instance.new("NumberValue",leaderstats)
	night.Name = "Night"
	night.Value = 1

	local Cheese = Instance.new("BoolValue",leaderstats)
	Cheese.Name = "Cheese"
	Cheese.Value = false
	
	local FirstPlayed = Instance.new("BoolValue",leaderstats)
	FirstPlayed.Name = "firstPlay"
	FirstPlayed.Value = true

	local data
	local success, err = pcall(function()
		data = myDataStore:GetAsync(player.UserId)
	end)

	if success then
		night.Value = data
		Cheese.Value = data
		FirstPlayed.Value = data
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, err = pcall(function()
		myDataStore:SetAsync(player.UserId,{player.leaderstats.Night.Value,player.leaderstats.Cheese.Value,player.leaderstats.firstPlay.Value})
	end)

	if success then
		print("ye")
	else
		warn(err)
	end
end)

replicatedStorage.First.OnServerEvent:Connect(function(player)
	local leaderstats = player.leaderstats
	local playValue = leaderstats.firstPlay
	
	playValue.Value = false
end)

:face_with_raised_eyebrow: is this correct?