Cloning a script from ReplicatedStorage to every ProximityPrompt within a Model?

The reason why it’s cloning a script into every part within the model is because you’re using tree:GetDescendants() which gets every part and all the other parts within those parts, and when you put it in a loop, it iterates through all those parts, thus creating a script in the parts.

And so to fix this, since you’ve already gotten all the tree models within the workspace, all you have to do is clone a script into treeModel.Prompt.
Replace this

		for _, part in ipairs(treeModel:GetDescendants()) do
			if part.Name == "WoodcuttingPrompt" then
				local newScript = woodcuttingScript:Clone()
				newScript.Parent = part
			end
		end

with this

woodcuttingScript:Clone().Parent = treeModel.Prompt -- A shortened way to clone an obj into another obj

|

Also, I suggest you use CollectionService, way more efficient than cloning a script into all of them.
Video Tutorial

Another thing to note, you don’t need to use ipairs() anymore as stated by this guy

so you can replace this for _,v in treeModel in ipairs(workspace:GetChildren()) do with this for _,v in treeModel in workspace:GetChildren() do and like wise for any other loop.

2 Likes