Datastore Player Has No Data

local playerUserId = "Player_"..player.UserId

	local data
	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(playerUserId)
	end)

	if success then
			Mana.Value = data.Mana
			LastName.Value = data.LastName
			Damage.Value = data.Damage
			MaxMana.Value = data.MaxMana
			Hunger.Value = data.Hunger
			MaxHunger.Value = data.MaxHunger
			Experience.Value = data.Experience
			Money.Value = data.Money
			Health.Value = data.Health
			MaxHealth.Value = data.MaxHealth
			WallStamina.Value = data.WallStamina
			MaxWallStamina.Value = data.MaxWallStamina
			Luck.Value = data.Luck
			HasRace.Value = data.HasRace
			Race.Value = data.Race
			NameRace.Value = data.NameRace
			HasNobility.Value = data.HasNobility
			NameNobility.Value = data.NameNobility
			Nobility.Value = data.Nobility
			Welcome.Value = data.Welcome
			Gender.Value = data.Gender
			Face.Value = data.Face
			Brows.Value = data.Brows
			Nose.Value = data.Nose
			Age.Value = data.Age
			Eye.Value = data.Eye
			SkinColor.Value = data.SkinColor
			FirstName.Value = data.FirstName
			MagicKnight.Value = data.MagicKnight
			Squad.Value = data.Squad
		HasCloak.Value = data.HasCloak
		print("Player has data")
	else
		HasRace.Value = false
		HasNobility.Value = false
		Welcome.Value = false
		HasCloak.Value = false
			Health.Value = defaultHealth
			Mana.Value = defaultMana
			LastName.Value = "None"
			Damage.Value = defaultDamage
		Hunger.Value = defaultHunger
		MaxHunger.Value = defaultMaxHunger
		Experience.Value = defaultExperience
		Money.Value = defaultMoney
		Health.Value = defaultHealth
		MaxHealth.Value = defaultMaxHealth
		WallStamina.Value = defaultWallStam
		MaxWallStamina.Value = defaultMaxWallStam
		Gender.Value = defaultGender
		Face.Value = defaultFace
		Brows.Value = defaultBrows
		Nose.Value = defaultNose
		Age.Value = defaultAge
		Eye.Value = defaultEye
		SkinColor.Value = defaultSkinColor
		FirstName.Value = "None"
		Squad.Value = "None"
		MagicKnight.Value = "None"
		
			print("Player has no data")
	end

	
end)

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

	local playerUserId = "Player_"..player.UserId

	local data = {
		Health = player.Health.Value;
		Mana = player.Mana.Value;
		LastName = player.LastName.Value;-- player.leaderstats.LastName.Value EXAMPLE
		MaxMana = player.MaxMana.Value;
		Hunger = player.Hunger.Value;
		MaxHunger = player.MaxHunger.Value;
		Experience = player.Experience.Value;
		Money = player.Money.Value;
		MaxHealth = player.MaxHealth.Value;
		WallStamina = player.WallStamina.Value;
		MaxWallStamina = player.MaxWallStamina.Value;
		Luck = player.Luck.Value;
		HasRace = player.HasRace.Value;
		Race = player.Race.Value;
		NameRace = player.NameRace.Value;
		HasNobility = player.HasNobility.Value;
		NameNobility = player.NameNobility.Value;
		Nobility = player.Nobility.Value;
		Welcome = player.Welcome.Value;
		Gender = player.Gender.Value;
		Face = player.Face.Value;
		Brows = player.Brows.Value;
		Nose = player.Nose.Value;
		Age = player.Age.Value;
		Eye = player.Eye.Value;
		SkinColor = player.SkinColor.Value;
		FirstName = player.FirstName.Value;
		MagicKnight = player.MagicKnight.Value;
		Squad = player.Squad.Value;
		HasCloak = player.HasCloak.Value
	}

	local success, errormessage = pcall(function()
		wait(Save_Interval)
		myDataStore:SetAsync(playerUserId, data)
	end)


	if success then
		print("Data successfully saved!")
	else
		print("There was an eror saving data!")
		warn(errormessage)
	end


end)

game:BindToClose(function()

	if RunService:IsStudio() then
		return
	end

	print("Saving Player data")


	local players = Players:GetPlayers()
	for _, player in pairs(players) do
		local playerUserId = "Player_"..player.UserId

		local data = {
			Health = player.Health.Value; -- Add THE CODE BELOW THISSSSSSSSSSSSSSSS ADD APOSTROPHE DONT ON LAST ONE
			Mana = player.Mana.Value; -- IF IT IT IN THE PLAYER PUT IT IN HERE IF IT IN LEADERSTATS DO THIS MAKE SURE TO DOUBLE CHECK
			LastName = player.LastName.Value;-- player.leaderstats.LastName.Value EXAMPLE
			MaxMana = player.MaxMana.Value;
			Hunger = player.Hunger.Value;
			MaxHunger = player.MaxHunger.Value;
			Experience = player.Experience.Value;
			Money = player.Money.Value;
			MaxHealth = player.MaxHealth.Value;
			WallStamina = player.WallStamina.Value;
			MaxWallStamina = player.MaxWallStamina.Value;
			Luck = player.Luck.Value;
			HasRace = player.HasRace.Value;
			Race = player.Race.Value;
			NameRace = player.NameRace.Value;
			HasNobility = player.HasNobility.Value;
			NameNobility = player.NameNobility.Value;
			Nobility = player.Nobility.Value;
			Welcome = player.Welcome.Value;
			Gender = player.Gender.Value;
			Face = player.Face.Value;
			Brows = player.Brows.Value;
			Nose = player.Nose.Value;
			Age = player.Age.Value;
			Eye = player.Eye.Value;
			SkinColor = player.SkinColor.Value;
			FirstName = player.FirstName.Value;
			MagicKnight = player.MagicKnight.Value;
			Squad = player.Squad.Value;
			HasCloak = player.HasCloak.Value
		}

		if data then
			local success, result = pcall(function()
				myDataStore:SetAsync(playerUserId, data)
			end)

			if not success then
				warn(result)
			end


		end
	end
	print("Completed saving player data!")

end)

I was wondering why my datastore wasnt working. Everytime the player has no data when they leave and rejoin the game. Any help would be appreciated.

you are checking if the Datastore request fails to reset their data(not if their data is empty) so if the pcall fails for a reason but data is there and then it saves on PlayerRemoving it will reset their data to the new values(Datastores can fail for many reasons but I guess you haven’t enabled studio API requests)

edit: you can see the reason it fails by doing print(errormessage)

2 Likes