Data Story problem

I’m trying to make a data store where when a player joined the game they will get a data, but when they joined the game the Console shows that the data is nil.

Here’s the script that I used, not the exact code but I hope you will understand it.

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

game.Players.PlayerAdded:Connect(function(plr)
	
	local Nickname = Instance.new("StringValue",plr)
	Nickname.Name = "Nickname"
	Nickname.Value = plr.Name
	
	plr.CharacterAdded:Connect(function(char)
		local playerUserId = "Data_"..plr.UserId
		-- Loading Character
		local data
		local success, errormessage = pcall(function()
			data = myDataStore:GetAsync(playerUserId)
		end)

		if success then
			Nickname.Value = data.Nickname
		end
	end)
end)

try

maybe print


Already, the problem is when other player joined the game their data is always Nil.

do you save using SetAsync
an example

local DataStoreService = game:GetService("DataStoreService")
local ds= DataStoreService:GetDataStore("myDataStore")
ds:SetAsync('i love Qin2007','Qin2007')
print(ds:GetAsync('i love Qin2007'))

If I SetAsync the data before the previous data GetAsync, it will erased the previous data. The data store is working in my character but in other character won’t work.

if you dont save the value will be nil
one time event

You should check if :GetAsync() returns nil, if it does then create new data for that player with :SetAsync() and use that new data.

perhap :sweat_smile:

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

game.Players.PlayerAdded:Connect(function(plr)
	
	local Nickname = Instance.new("StringValue",plr)
	Nickname.Name = "Nickname"
	Nickname.Value = plr.Name --default value
	
	local playerUserId = "Data_"..plr.UserId
		-- Loading Character
		local data
		local success, errormessage = pcall(function()
			data = myDataStore:GetAsync(playerUserId)
		end)

		if success then
			if data ~= nil then --check if data is not a nil value
				Nickname.Value = data.Nickname
			else
				print("New Player!) 
				--if new player datastore will return nil
				--default value will be used instead which is playerName
			end
		end
	
	plr.CharacterAdded:Connect(function(char)
		
	end)
end)
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
local players = game:GetService("Players")

players.PlayerAdded:Connect(function(plr)
	local Nickname = Instance.new("StringValue")
	Nickname.Name = "Nickname"
	Nickname.Value = plr.Name
	Nickname.Parent = plr
	plr.CharacterAdded:Connect(function(char)
		local playerUserId = "Data_"..plr.UserId
		local data
		local success, errormessage = pcall(function()
			data = myDataStore:GetAsync(playerUserId)
		end)
		if success then
			Nickname.Value = data
		end
	end)
end)

data.Nickname would be nil in your original post.