For loop runs but the instance doesn't get parented?

so i’ve got this datastore code, and yay it works!

but for some reason, the clothing code runs, including all of the prints, but the actual clothing doesn’t
even get parented, why??

i’ve tried removing the RemovePreviousClothingWithSameType, but it doesn’t fix it either,
i tried putting everything in a .CharacterAdded event, then the entire for loop just doesn’t run at all

what the hell is going on??

all of this code is inside a .PlayerAdded event,
the variables are correct, so what’s going on??

for clothingType, clothingName in pairs(result.Clothing) do
		playerDataTable[plr.UserId].Clothing[clothingType] = clothingName
			
		-- and also apply it to the player's character
		-- but first we need to destroy it
		local clothingToClone = GetClothingFromName(clothingName)
		local clothing = clothingToClone:Clone()
			
		RemovePreviousClothingWithSameType(characterClothesFolder, clothingType)
		clothing.Parent = characterClothesFolder
		print(clothing.Parent) -- prints out "Clothes", which is correct
        -- but when i look into workspace, the folder is empty???
		print(clothing)
end

Can you show the entire scrript?

Only thing i can think of is the Clothes folder is different to the one in workspace

it’s a bit long :grimacing:

but sure!

it’s not the entire script,
it’s just the function that initializes player data, as the name suggests

function InitializePlayerData(plr: Player)
	local totalKills = GetOrCreateAttribute(plr, "TotalKills", 0)
	local totalDeaths = GetOrCreateAttribute(plr, "TotalDeaths", 0)
	local totalCurrencyEarned = GetOrCreateAttribute(plr, "TotalCurrencyEarned", 0)
	local totalVoidstonesEarned = GetOrCreateAttribute(plr, "TotalVoidstonesEarned", 0)
	local totalXpEarned = GetOrCreateAttribute(plr, "TotalXpEarned", 0)
	local currentLevel = GetOrCreateAttribute(plr, "CurrentLevel", 1)
	local currentCurrency = GetOrCreateAttribute(plr, "Zetabonds", 0)
	local currentVoidstones = GetOrCreateAttribute(plr, "Voidstones", 0)
	local currentXp = GetOrCreateAttribute(plr, "CurrentXp", 0)
	local currentClass = GetOrCreateAttribute(plr, "CurrentClass", "Survivor")
	local currentWhisper = GetOrCreateAttribute(plr, "CurrentWhisper", 0) 
	-- 'Whisper' is basically a rebirth, but with an edgy name
	-- so think of 'Whisper' as 'Rebirth' if you find it easier

	local success, result = pcall(function()
		return playerDataStorage:GetAsync(plr.UserId)
	end)

	-- some player variables;
	local char = plr.Character
	local characterClothesFolder: Folder = char:FindFirstChild("Clothes")

	if not success then
		warn("Failed to fetch " .. plr.UserId .. "'s data!")
		warn("Error log: " .. result)

		result = nil
	end


	if not playerDataTable[plr.UserId] then
		-- Set data;
		playerDataTable[plr.UserId] = {
			TotalKills = totalKills,
			TotalDeaths = totalDeaths,
			TotalCurrencyEarned = totalCurrencyEarned,
			TotalVoidstonesEarned = totalVoidstonesEarned,
			TotalXpEarned = totalXpEarned,
			CurrentLevel = currentLevel,
			CurrentCurrency = currentCurrency,
			CurrentVoidstones = currentVoidstones,
			CurrentXp = currentXp,
			CurrentClass = currentClass,
			CurrentWhisper = currentWhisper,
			Settings = {
				-- Default values
				["Field of View"] = 90, 
				["Classic Aim Down Sights"] = false,
				["Vitals GUI Color"] = Color3.fromRGB(255,255,255),
				["Weapon GUI Color"] = Color3.fromRGB(255,255,255),
				["Visible Viewmodel"] = true,
				["Show Crosshair"] = true,
				["First Person Tracers"] = true,
				["Show Blood"] = true,
				["Show Rewards"] = true
			},
			Clothing = {
				-- Default values
				["tops"] = "StarterTop_Shirt",
				--["bottoms"] = "StarterBottom_Pants"
			}
		}
	end

	if result then
		playerDataTable[plr.UserId].TotalKills = result.TotalKills or totalKills
		playerDataTable[plr.UserId].TotalDeaths = result.TotalDeaths or totalDeaths
		playerDataTable[plr.UserId].TotalCurrencyEarned = result.TotalCurrencyEarned or totalCurrencyEarned
		playerDataTable[plr.UserId].TotalXpEarned = result.TotalXpEarned or totalXpEarned
		playerDataTable[plr.UserId].CurrentLevel = result.CurrentLevel or currentLevel
		playerDataTable[plr.UserId].CurrentWhisper = result.CurrentWhisper or currentWhisper
		playerDataTable[plr.UserId].CurrentCurrency = result.CurrentCurrency or currentCurrency
		playerDataTable[plr.UserId].CurrentVoidstones = result.CurrentVoidstones or currentVoidstones
		playerDataTable[plr.UserId].CurrentXp = result.CurrentXp or currentXp
		playerDataTable[plr.UserId].CurrentClass = result.CurrentClass or currentClass

		local vitalsGuiColorDeserialized = Deserialize(result.Settings["Vitals GUI Color"])
		local weaponGuiColorDeserialized = Deserialize(result.Settings["Weapon GUI Color"])

		-- change the settings blah blah
		for setting, value in pairs(result.Settings) do
			playerDataTable[plr.UserId].Settings[setting] = value
		end
		
		-- then we change the clothing
		for clothingType, clothingName in pairs(result.Clothing) do
			playerDataTable[plr.UserId].Clothing[clothingType] = clothingName
			
			-- and also apply it to the player's character
			-- but first we need to destroy it
			local clothingToClone = GetClothingFromName(clothingName)
			local clothing = clothingToClone:Clone()
			
			RemovePreviousClothingWithSameType(characterClothesFolder, clothingType)
			clothing.Parent = characterClothesFolder
			print(clothing.Parent)
			print(clothing)
			print(characterClothesFolder.Parent)
			print(characterClothesFolder.Parent.Parent)
		end

		playerDataTable[plr.UserId].Settings["Vitals GUI Color"] = vitalsGuiColorDeserialized
		playerDataTable[plr.UserId].Settings["Weapon GUI Color"] = weaponGuiColorDeserialized

		plr:SetAttribute("TotalKills", playerDataTable[plr.UserId].TotalKills)
		plr:SetAttribute("TotalDeaths", playerDataTable[plr.UserId].TotalDeaths)
		plr:SetAttribute("TotalCurrencyEarned", playerDataTable[plr.UserId].TotalCurrencyEarned)
		plr:SetAttribute("TotalVoidstonesEarned", playerDataTable[plr.UserId].TotalVoidstonesEarned)
		plr:SetAttribute("TotalXpEarned", playerDataTable[plr.UserId].TotalXpEarned)
		plr:SetAttribute("CurrentLevel", playerDataTable[plr.UserId].CurrentLevel)
		plr:SetAttribute("CurrentWhisper", playerDataTable[plr.UserId].CurrentWhisper)
		plr:SetAttribute("Zetabonds", playerDataTable[plr.UserId].CurrentCurrency)
		plr:SetAttribute("Voidstones", playerDataTable[plr.UserId].CurrentVoidstones)
		plr:SetAttribute("CurrentXp", playerDataTable[plr.UserId].CurrentXp)
		plr:SetAttribute("CurrentClass", playerDataTable[plr.UserId].CurrentClass)
	end

	returnPlayerSettings:FireClient(plr, playerDataTable[plr.UserId].Settings)
end

Aight so, 2 problems;

  1. you dont use character added, which should be done as this:
local function newCharacter()
--- code here
end

if player.Character then newCharacter() end
player.CharacterAdded:Connect(newCharacter)

anddd
2. you make a refrence to the clothing folder, but if the player gets a new character, it’s a different folder, so you need to update the variable or just dont use one

!!!

the problem is the fact the clothing doesn’t even exist in the first character

it gets parented, and then it just disappears

Try welding/anchoring it before it gets parented to the workspace. It sounds like they’re falling into the void and getting destroyed.

1 Like

i told you how to do it properly