Pasting a script into all selected parts?

I created a car, and I need to paste a script into every part of this car. It has over 100 parts, so it would be tedious to paste a script into every one. If I select all the parts, ‘paste into’ is greyed out. Is there a command I can use, or do I need to do this manually? Thanks

1 Like

Couldn’t you just use one script to control all the parts?

1 Like

Yes like @AWhale_OfATime said you could do something like:

local car = -- define
for i, part in pairs(car:GetDescendants()) do
      if part:IsA("BasePart") then
            --code here 

             part.Touched:Connect(function(hit)
                    print(part.Name.." Was Touched By: "..hit.Name)
             end)
      end
end
2 Likes

You can highlight all the parts and paste it in that way, but before you do that is there a better way instead of using 100 scripts? I feel that could cause some stress on the server, try to use a for loop to iterate through all the parts and have them do what you want them to do.

1 Like

I am making a demo derby, and I need to use a script that removes welds of anything that runs into it. If I just put the script into the bumper, then when the bumper falls off, the parts no longer get unwelded.

1 Like

Make a model of the car and inside that model add a script that controls the model’s descendants. That way no matter what parts fall off the car there will still be one script controlling it.

1 Like

So, it would look like:

model = script.Parent

parts=model:GetChildren

then if parts touched, unweld, ect.?

It would be like:

for _,part in pairs(script.Parent:GetChildren()) do
   part.Touched:Connect(function()
       if part:IsA("BasePart") then
          --unweld
       end
   end)
end
1 Like

Ok, thanks. I’ll try it. I guess I didn’t think of having just one script in the model.

1 Like

I forgot to add an if statement checking if the child is a part. I just edited it, remember to add that.

Sorry, one more thing… I’m not very good at scripting, so would you mind pointing out my error (probably a simple one)?

‘’’

for i, part in pairs(script.Parent:GetChildren()) do
function onTouched(hit)
	if not hit or not hit.Parent then return end
	local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
	if humanoid then 

	else
		hit:BreakJoints()
	end
end

script.Parent.Touched:connect(onTouched)

end

‘’‘’

Although I don’t think having so many scripts is a good idea, if you’re selecting the targets by mouse then you can just input:

Windows: Ctrl + Shift + V
Mac: Cmd + Shift + V

This is if you copied the script already using Ctrl+C (Windows) or Cmd+C (Mac) or Right-Click+Copy

Thanks! I’ll use this, I think, until I can get my other script working.

I might be late, but you can use Selection:Get()

for i, v in pairs(game:GetService("Selection"):Get()) do
    if v:IsA("BasePart") then
        local Script = Instance.new("Script")
        Script.Source = " -- something here"
        Script.Parent = v
    end
end

It will copy the script in every basepart that is selected

  • 1, Copy the script!
  • 2, Select all the parts!
  • 3, Press: “Control + Shift + V”

After you complete the steps above your script will be in the all the parts!