Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

how to make all parts that are named the same transparent?

Basically I have 4 parts in workspace named the same and only one of them become transparent when I want all of them to be. How can I fix?

dq = game.Workspace.Party


function onTouched(hit)
	local humanoid = hit.Parent:findFirstChild("Humanoid")
	if humanoid~=nil then 
	dq.Transparency = 1
	dq.CanCollide = false
	end
end

script.Parent.Touched:connect(onTouched)

1 answer

✓ Accepted answer

You have a few options. You can:

  1. Keep the parts in a model and loop through the model

  2. Loop through the workspace and find all objects named tag

  3. Use collection service to tag all the parts and looped thru the tagged parts. This is more for bigger projects with hundreds of items you might not want to have in one model. If you're interested about it, read here for a nice tutorial

#Option 1:

local model = workspace.PartyParts

function onTouched(hit)
	local humanoid = hit.Parent:findFirstChild("Humanoid")

	if humanoid then 
		--loop thru all items in the model and change children named 'Party''s transparency/collision 

		for _, child in pairs(model:GetChildren())do
			if child.Name == "Party" then
				child.Transparency = 1
				child.CanCollide = false
			end
		end
	end
end

script.Parent.Touched:Connect(onTouched)

#Option 2:


function onTouched(hit)
	local humanoid = hit.Parent:findFirstChild("Humanoid")

	if humanoid then 
		--loop thru all items in the workspace and change children named 'Party''s transparency/collision 
		for _, child in pairs(workspace:GetChidren())do
			if child.Name == "Party" then
				child.Transparency = 1
				child.CanCollide = false
			end
		end 
	end
end

script.Parent.Touched:Connect(onTouched)


2 comments

0

Thank you

0

np let me know if you need anything else

This discussion is preserved as it was. New answers, comments, and votes are closed.