Datastore failing to find stats in the character

hey, so I’ve ran into a problem with Datastores where it wont save when a person leaves, didnt try manual saving yet, but here it is

image

for some reason it can not locate the “AllStats” folder even though it is there

game.Players.PlayerAdded:Connect(function(player)
	
	player.CharacterAdded:Connect(function(character)
		
		local allStatuses = Instance.new("Folder")
		allStatuses.Name = "AllStatuses"
		allStatuses.Parent = character
		
		local auraStatus = Instance.new("StringValue")
		auraStatus.Value = "Off"
		auraStatus.Name = "AuraStatus"
		auraStatus.Parent = allStatuses
		
		local formStatus = Instance.new("StringValue")
		formStatus.Value = "Base"
		formStatus.Name = "FormStatus"
		formStatus.Parent = allStatuses
		
		local allStats = Instance.new("Folder")
		allStats.Name = "AllStats"
		allStats.Parent = character
	
		local s1 = Instance.new("IntValue")
		s1.Name = "Strength"
		s1.Parent = allStats
	
		local s2 = Instance.new("IntValue")
		s2.Name = "Defense"
		s2.Parent = allStats
	
		local s3 = Instance.new("IntValue")
		s3.Name = "Ki"
		s3.Parent = allStats
	
	
		local strdata
		local defdata
	
	local success, failure = pcall(function()
		strdata = statStore:GetAsync(player.UserId.."-strength")
	end)
		
	if success then
		s1.Value = strdata
		print("Data loaded successfully")
	else
		print("Failed to load data")
		warn(failure)
	end

	end)
	
end)



game.Players.PlayerRemoving:Connect(function(player)
	
	local success, failure = pcall(function()
		statStore:SetAsync(player.UserId.."-strength",player.Character.AllStats.Strength.Value) --doesnt save here
	end)
	
	if success then
		print("Data Saved")
	else
		print("Failed")
		warn(failure)
	end
	
end)


game:BindToClose(function(player)
    
	local success, failure = pcall(function()
		statStore:SetAsync(player.UserId.."-strength",player.Character.AllStats.Strength.Value)
	end)

end)

I had the same problem before but I fixed it, all I did was rather adding the Child right away I used “FindFirstChild”.

In your code, you can apply this by

local allstats = player.Character:FindFirstChild("AllStats")
if allstats then
    statStore:SetAsync(player.UserId.."-strength",allstats.Strength.Value)
end

If FindFirstChild doesn’t help try WaitForChild. Hopefully it works!

I don’t think storing stats in the player’s character is a good idea because character can be destroyed

they are not as stable as player

The folder’s name is AllStatuses not AllStats.

1 Like

sadly, i’ve tried that, waitforchild waits infinitely and findfirstchild comes back with the same error.

there are 2 different folders, allstatuses isnt a part of the saving system

oh that actually might be the issue since if the character is destroyed (when leaving) allstats can not exist

yep!! saving the stats in player instead of character solved the issue