DataStore problem

I making a game with Data Store service and i have a problem with saving/loading Data…
its not giving me any error its giving me nothing in output so i dont know where and how did i missed up

local DataStore = game:GetService("DataStoreService")
local StandDataStore = DataStore:GetDataStore("StandDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local PlayerUserId = "Player_"..player.UserId
	local Data
	local StandData = Instance.new("IntValue")
	StandData.Name = "StandData"
	StandData.Parent = player
	local success, errormessage = pcall(function()
		Data = StandDataStore:GetAsync(PlayerUserId)
	end)
	if success then
		StandData.Value = Data
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local PlayerUserId = "Player_"..player.UserId
	local Data = player.StandData.Value
	
	local success, errormessage = pcall(function()
		StandDataStore:SetAsync(PlayerUserId, Data)
	end)
	if success then
		print(PlayerUserId.." data has been saved successfuly! his stand id is = "..Data)
	elseif errormessage then
		print("there was an error with "..PlayerUserId.." Data!")
		warn(errormessage)
	end
	
end)

Thanks for trying to help

Try:

game.Players.PlayerRemoving:Connect(function(player)
	local PlayerUserId = "Player_"..player.UserId
	local Data = player.StandData
	
	local success, errormessage = pcall(function()
		StandDataStore:SetAsync(PlayerUserId, Data.Value)
	end)
	if success then
		print(PlayerUserId.." data has been saved successfuly! his stand id is = "..Data)
	elseif errormessage then
		print("there was an error with "..PlayerUserId.." Data!")
		warn(errormessage)
	end
	
end)
1 Like

thanks you! its finally worked!