Error datastoring the value of a StringValue. The value goes nil

  1. What do you want to achieve? Keep it simple and clear!
    I was trying to get a random name from a table/array (this function is in a module) when the function of the module is requested it will return the name obtained from the array and the value of a StringValue will be set to that name.

  2. What is the issue?
    The issue is that when the player is leaving and I want to datastore the value of my StringValue it will appear as nil. If I then go into the game and check the value on the player or on the server the value is clearly there, but once I leave it for some reason goes to nil. I’m data storing more than one value, and these other values datastore correctly and don’t go to nil unlike “name”.

Module:

local module = {}

local NamesM = {"Edward", "Alfred", "Walter", "Anderson", "Andrew", "Xander", "Giotto", "Pietro", "Louis", "Claude", "Gaston",
	"Antonio", "Armand", "Leon", "Sergio", "Bruno", "Alvaro", "Dario", "Noe", "Roy", "Maes", "Alex", "Selim", "Jean", "Sig", "Berthold",
	"Artemy", "Daniil", "Ivan", "Vladimir", "Igor", "Alexander", "Andrei", "Akim", "Adrik", "Pip", "Jan", "Luke", "Arthur", "William", "Albert",
}

local NamesF = {"Integra", "Natasha", "Elicia", "Sarah", "Gracia", "Catherine", "Trisha", "Rose", "Izumi", "Olivia",
	"Seras", "Rip", "Angelica", "Amber", "Ruby", "Nemow", "Anastasia", "Anya", "Irina", "Tatiana", "Vera", "Anna", "Ilya", "Angelina", "Winry", "Rebecca", "Connie", "Alva", "Marie", "Rachel", "Rosalind", "Lise", "Alise", "Mary", "Asima", "Dorothy", "Vera", "Katherine", "Jennifer"
}

local LastNames = {"Armstrong","Dornez","Hellsing","Winkle","Elric","Wagner","Phillips","Klein","Smith","Mustang","Tuffin","Thomas","Katz","Madden","Laurier","Romano","Fischer","Ivanov","Petrof","Smirnoff","Burak","Mellow","Croy","Dankovsky","Lee","Campbell","Elliot","Bowes","Dal","Moen","Rud","Hasen","Johansen","Stanley","Tudor","Markle","Howard", "Eno", "Tsal", "MAxwell", "Valentine", "Max", "Einstein", "Burnell", "King", "Doudna", "Freese", "Bishop", "Victoria", "Widdowson", "Curie"
}

function module.NameChooser(FM)
	local Name = 0
	local Surname = 0
	
	if FM == "M" then
		Name = NamesM[math.random(1, #NamesM)]
	elseif FM == "F" then
		Name = NamesF[math.random(1, #NamesF)]
	end
	
	Surname = LastNames[math.random(1, #LastNames)]
	return Name.." "..Surname
end

return module

Server script:

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")

local DataStore = DataStoreService:GetDataStore("DataStoreTest")

local RaceModule = require(script.RaceChooser)
local NameModule = require(script.NameModule)

function SetUpData(Player: Player)
	coroutine.wrap(function()
		local id = Player.UserId

		local data = DataStore:GetAsync(id) or {}

		local PlayerStats = script.Stats:Clone()
		PlayerStats.Parent = Player
		
		
		PlayerStats:WaitForChild("Gender").Value = data.Gender or "M"
		PlayerStats:WaitForChild("Money").Value = data.Money or 50
		PlayerStats:WaitForChild("Race").Value = data.Race or RaceModule.GetRace()
		PlayerStats:WaitForChild("Name").Value = data.Name or GeneralStuff.NameChooser(PlayerStats.Gender.Value)
	end)()
end


function SaveData(Player: Player)
	local id = Player.UserId

	local PlayerStats = Player:FindFirstChild("Stats")
	print(PlayerStats.Name.Value) -- I try to print the value of name to check what is it equal to and it says nil.
       print(PlayerStats.Health.Value) -- I print any other value, and it returns the value that it is equal to.

	if PlayerStats then
		local success, ret = pcall(DataStore.SetAsync, DataStore, id, {
			Gender = PlayerStats.Gender.Value,
			Name = PlayerStats.Name.Value, 
			Race = PlayerStats.Race.Value,
			Money = PlayerStats.Money.Value, 		
		})
	end
end

Players.PlayerAdded:Connect(SetUpData)
Players.PlayerRemoving:Connect(SaveData)

while true do
	wait(60)
	for _,players in ipairs(Players:GetPlayers()) do
		coroutine.wrap(SaveData)(players)
	end
end

And here’s is how the properties of the stringvalue appear on client and server, the value is there.

But I print it when saving the data and it is nil.
image

Hope someone can help me with this. :smile:

Turns out I’m bad at paying attention to details. “Name” is not only the name that I gave the StringValue, but also a property of the folder that value was in. I was indexing the name of the folder instead of the value inside of it.