I am trying to create a leaderstats using datastore2. It works half the time and creates the folder but for some reason the other half of the time it doesnt create it and just does nothing. Ideas?

local DataManager = {}

local DataStore2 = require(1936396537)
local HttpService = game:GetService("HttpService")

local DefaultPlayerTable = HttpService:JSONEncode({Rank = "Peasent", Strength = 0, Coins = 0, GoldBars = 0})

local DefaultTable = HttpService:JSONEncode({})

local function ResetData(player)
	DataStore2("PlayerStore", player):Set(DefaultPlayerTable)
end

game.Players.PlayerAdded:Connect(function(player)
	
	print("called")
	
	local leader = Instance.new("Folder")
	leader.Parent = player
	leader.Name = "leaderstats"
	
	local PlayerStats = Instance.new("StringValue")
	PlayerStats.Name = "PlayerStats"
	PlayerStats.Parent = leader
	PlayerStats.Value = DataStore2("PlayerStore", player):Get(DefaultPlayerTable)
	
	local UnlockedIslands = Instance.new("StringValue")
	UnlockedIslands.Name = "UnlockedIslands"
	UnlockedIslands.Parent = leader
	UnlockedIslands.Value = DataStore2("UnlockedIslands", player):Get(DefaultTable)
	
	DataStore2("PlayerStore", player):OnUpdate(function(newValue)
		PlayerStats.Value = newValue
	end)
	
	DataStore2("UnlockedIslands", player):OnUpdate(function(newValue)
		UnlockedIslands.Value = newValue
	end)


	DataManager:SetData("UnlockedIslands", "GrassyIsland", true, player)	
	
	--sets the value of strength to 10
	--sets the rank to Peasent
	DataManager:SetData("PlayerStore", "Strength", 10, player)
	DataManager:SetData("PlayerStore", "Rank", "Peasant", player)

	--gets the value of strength from the player
	DataManager:GetData("PlayerStore", "Strength", player)
end)

function DataManager:GetData(Store, DataName, player)
	if Store == "PlayerStore" then
		local data = HttpService:JSONDecode(DataStore2("PlayerStore", player):Get(DefaultPlayerTable))
		return data[DataName]
	elseif Store == "UnlockedIslands" then
		local data = HttpService:JSONDecode(DataStore2("UnlockedIslands", player):Get(DefaultTable))
		return data[DataName]
	end
end

function DataManager:SetData(Store, valueName, newValue, player)
	if Store == "PlayerStore" then
		local data = HttpService:JSONDecode(DataStore2("PlayerStore", player):Get(DefaultPlayerTable))
		if data[valueName] then
			data[valueName] = newValue
			local newTable = HttpService:JSONEncode(data)
			DataStore2("PlayerStore", player):Set(newTable)
		else
			warn(valueName.." does not exist")
		end
	elseif Store == "UnlockedIslands" then
		local data = HttpService:JSONDecode(DataStore2("UnlockedIslands", player):Get(DefaultTable))
		data[valueName] = newValue
		local newTable = HttpService:JSONEncode(data)
		DataStore2("UnlockedIslands", player):Set(newTable)
	end
end



return DataManager