How do I check if the data of the player is empty / if it's his first time playing the game?

I want to know if the player’s data is empty or not. But I do not know how to check if the data is empty. I have tried looking on the DevForum but didn’t find anything and I did not find any tutorials on what I’m trying to achieve.

Here is my code :

players.PlayerAdded:Connect(function(player)
	local agility = Instance.new("NumberValue", player.Character or player.CharacterAdded:Wait())
	agility.Name = "Agility"
	agility.Value = player.Character.Humanoid.WalkSpeed
	
	local race = Instance.new("StringValue", player)
	race.Name = "Race"
	
	local playerUserId = "Player_"..player.UserId
	
	-- Load Data
	local data
	
	local success, errormessage = pcall(function()
		data = dataStore:GetAsync(playerUserId)
	end)
	
	if success then
		race.Value = randomRace
	end
end)

players.PlayerRemoving:Connect(function(player)
	local playerUserId = "Player_"..player.UserId
	
	local data = {
		Race = player.Race.Value,
		
	}
	
	local success, errormessage = pcall(function()
		dataStore:SetAsync(playerUserId, data)
	end)
	
	if success then
		print("Data Saved!")
	else
		print("Error when saving Data!")
		warn(errormessage)
	end
end)

So right here I want to set a random race to the player, but only for the first time he joins. After that, I’m gonna set his value to the data.Race.

	if success then
		race.Value = randomRace
	end

I am grateful to anyone that can help me!

1 Like

I would suggest reading the documentation page of data stores.

Hey, hope you’re keeping well. Earlier this year, I discovered ProfileService and have been using it since. It’s really quite helpful and easy to understand after you’ve looked into it a bit.

If you want to learn more, you can read more here

Thank You! I found what I was looking for.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.