DataStore won't load values?

When I join I get the error message "Script:48: attempt to index nil with ‘Points’ " ( At playerjoined when the variables are assigned their values in the for loop)
I can use the values ingame correctly, but for some reason it won’t get saved/loaded as they just reset to 0 when rejoining.
Does anyone have an Idea why this might be?

--//SERVICES\\
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
local playerData = DataStoreService:GetDataStore("PlayerData")

--//FUNCTIONS\\
local function playerJoined(player)
	
	local cloud = Instance.new("Folder")
	cloud.Name = "cloud"
	cloud.Parent = player
	
	local points = Instance.new("IntValue")
	points.Name = "Points"
	points.Parent = cloud
	
	local strength = Instance.new("IntValue")
	strength.Name = "Strength"
	strength.Parent = cloud
	
	local defense = Instance.new("IntValue")
	defense.Name = "Defense"
	defense.Parent = cloud

	local speed = Instance.new("IntValue")
	speed.Name = "Speed"
	speed.Parent = cloud
	
	local mana = Instance.new("IntValue")
	mana.Name = "Mana"
	mana.Parent = cloud
	
	local superpower = Instance.new("StringValue")
	superpower.Name = "SuperPower"
	superpower.Parent = cloud

	local data 
	local success, errormessage =  pcall(function()
		data = myDataStore:GetAsync(player.UserId) 
	end)

	if success then
		--if Data is found i.e player is not new.
		for i,value in pairs(player.cloud:GetChildren()) do--Assume you have a folder with the values inside
			value.Value = data[value.Name]
		end

	else
		print("There was an error loading your data")
		warn(errormessage)
	end
end

local function createTable(player)
	local playerStats = {}
	for _, stat in pairs(player.cloud:GetChildren()) do

		playerStats[stat.Name] = stat.Value

	end
	return playerStats

end



local function playerLeft(player)  --Runs when players exit

	local playerStats = createTable(player)

	local success, err = pcall(function()

		playerData:SetAsync(player.UserId, playerStats) --Saves player data

	end)

	if not success then

		warn('Could not save data!')

	end

end
--//CONNECTS\\
game.Players.PlayerAdded:Connect(playerJoined)
game.Players.PlayerRemoving:Connect(playerLeft)

I’m not sure why the pcall doesn’t work, but it seems that even if you are wrapping the loop in a pcall it still retrieves nil and returns it as success (I tried the script in my own game), so the script tries to find the values in a data that is actual inexistant. The best thing you can do is to replace the pcall with:

    if myDataStore:GetAsync(player.UserId) ~= nil then
      -- Insert the loop here instead
    end

Now I don’t have the error anymore, but my data still won’t save and gets resetted to 0 when i rejoin : /

I tried doing some debugging, and apparently, the data doesn’t save as the function runs after the server has been shutdown (since there are no other players left). You can do it by yourself by adding a print inside of the playerLeft function

local function playerLeft(player)  --Runs when players exit

	local playerStats = createTable(player)

	local success, err = pcall(function()

		playerData:SetAsync(player.UserId, playerStats) --Saves player data

	end)

	if not success then

		warn('Could not save data!')

	end
     print("Player left") -- This won't print
end

What I’d do is save the data when the values change instead of when the player leaves. Maybe it’s just a Studio thing, try in normal Roblox and see if it still doesn’t save.

When I try that in studio it gets printed, I think the problem is something else

There’s nothing wrong with the script tho. And that’s weird, it doesn’t print for me. Well at this point I don’t really know how to help you, sorry bud. Good luck with it tho.

After debugging for 3 hours now i found the error. I used myDataStore when the player joined, but uploaded the data to playerData when the player left. lmao