Part Finding Problem

I have a script that detects when a part is clicked, then changes the transparency of a part.

local player = game.Players.LocalPlayer

local db = false
local Num = 1

local function InvAll(obj)
	local shishMeshes = {
		obj["Tactical Vest"].Shish1,
		obj["Tactical Vest"].Shish2,
		obj["Tactical Vest"].Shish3
	}

	for _, shishMesh in ipairs(shishMeshes) do
		if shishMesh then
			shishMesh.Transparency = 1
			wait(0)
		else
			print("Shish meshes not found or error occurred.")
		end
	end
end

local function ConnectClickDetector(player)
	local ClickDetector
	local success, result = pcall(function()
		ClickDetector = workspace:WaitForChild("ShishClicker"..player.Name).ClickDetector
	end)

	if success then
		print("ClickDetector Found")
		ClickDetector.MouseClick:Connect(function()
			print("ShishClicker Clicked")
			if not db then
				db = true

				local character = player.Character or player.CharacterAdded:Wait()

				local success, shishClicker = pcall(function()
					return workspace:FindFirstChild("ShishClicker"..player.Name)
				end)

				if not success then
					print("Error finding ShishClicker:", shishClicker)
				end

				if success and shishClicker then
					if character and character:FindFirstChild("Humanoid") and character:FindFirstChild("Tactical Vest") and shishClicker.Name == "ShishClicker"..player.Name then
						local vest = character["Tactical Vest"]

						if Num == 1 then
							vest.Shish1.Transparency = 0
							Num = 2
						elseif Num == 2 then
							vest.Shish2.Transparency = 0
							vest.Shish1.Transparency = 1
							Num = 3
						elseif Num == 3 then
							vest.Shish3.Transparency = 0
							vest.Shish2.Transparency = 1
							Num = 4
						elseif Num == 4 then
							InvAll(character)
							Num = 1
						end
					elseif not character then
						print("Character not found")
					elseif not character:FindFirstChild("Humanoid") then
						print("Humanoid not found in character")
					elseif not character:FindFirstChild("Tactical Vest") then
						print("Tactical Vest not found in character")
					elseif shishClicker.Name ~= "ShishClicker"..player.Name then
						print("ShishClicker name does not match")
					end

					wait(0.1)
					db = false
				end
			end
		end)
	end
end

ConnectClickDetector(game.Players.LocalPlayer)

local vest = player:WaitForChild("Tactical Vest")
vest.Equipped:Connect(function()
	wait(1)
	ConnectClickDetector(game.Players.LocalPlayer)
end)

The problem is that once the vest unequipped, then reequipped, because the part was parented to the vest before being reparented, the script can’t find the part again, even though it is in the workspace.
Here is the script that controls the vest equipping:

local pPrompt = script.Parent
local partToWeldTo = "Torso" -- What part of the player you want to morph the model to
local item = script.Parent.Parent.Parent.Parent:FindFirstChildWhichIsA("Model") -- Model your wanting to morph to the player. Can also be reaplced with:local item = script.Parent.Parent.Parent.Parent:WaitForChild("NameOfItemHere")

function addMorph(player)
	local g = item:Clone()
	local C = g:GetChildren()
	for i=1, #C do
		if C[i].className == "Part" or C[i].className == "UnionOperation" or C[i].className == "MeshPart" then
			local W = Instance.new("Weld")
			W.Part0 = g.Middle
			W.Part1 = C[i]
			local CJ = CFrame.new(g.Middle.Position)
			local C0 = g.Middle.CFrame:inverse()*CJ
			local C1 = C[i].CFrame:inverse()*CJ
			W.C0 = C0
			W.C1 = C1
			W.Parent = g.Middle
		end
	end
	local Y = Instance.new("Weld")
	Y.Part0 = player.Character:FindFirstChild(partToWeldTo)
	Y.Part1 = g.Middle
	Y.C0 = CFrame.new(0, 0, 0)
	Y.Parent = Y.Part0
	local h = g:GetChildren()
	for i = 1, # h do
		h[i].Anchored = false
		h[i].CanCollide = false
	end
	g.Parent = player.Character
end


pPrompt.Triggered:Connect(function(player)
	local sameVest = false
	for _,v in pairs(player.Character:FindFirstChild(partToWeldTo):GetChildren()) do
		if v:IsA("Weld") then
			if v.Part1.Parent.Name == item.Name then
				sameVest = true
			end
			v.Part1.Parent:Destroy()
			v:Destroy()
		end
	end
	if sameVest == false then
		addMorph(player)
	end
end)

I have tried looking on the DevForum, but nothing I have found will help.

If you store an Instance in a variable it will always point to that Instance, doing it that way might be better than looking for the part by name.

I added instances too, but I also had to add some other functions to continuously find the ShishClickers, after the first one was destroyed.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.