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

How can I clone a script into every single part?

Hello! currently trying to clone a Script into every part in the workspace, Such as Models, and Single Parts, how would I do this?

2 answers

✓ Accepted answer
local allObjects = game.Workspace:GetChildren()
local myScript = game.Workspace.myScript

for _, object in pairs(allObjects) do
	if object:IsA("Part") or object:IsA("Model") then
		local partScript = myScript:Clone()
		partScript.Parent = object
	end
end

"game.Workspace:GetChildren()" returns a table of all the objects. Then in the loop, you check if it is a part or model. If it is, clone your script and set the parent to the part.

You can also set the workspace to listen for a "ChildAdded" event and call a function to assign the script to every new part added.

script = //script -- specify(?) the script, i don't know the proper term

for i,v in pairs(workspace:GetDescendants()) do
if v:IsA("Part") then
script:Clone().Parent = v
end
end)

That's the general idea, by using the for i,v in pairs() loop.

:GetDescendants() would get every single instances, models, scripts, lights, everything. So; you need to specify what the script would get cloned into with :IsA()

I'd imagine that your "every single part" is not necessarily just "part(s)", it might be a whole building, I don't know what you're thinking.

if that's the case, you might want to do

if v:IsA("BasePart") or v:IsA("UnionOperation") then

BasePart would select all the "special" parts, the cornerwedgepart, wedgepart, and such.

Well, everything else is pretty much up to your creativity and self-explanatory.

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