Trying to clone a script from replicated storage to every Tree.Prompt.WoodcuttingPrompt in my game, the reason is so that if I were to change anything with the script, I wouldn’t have to go through every tree in the game and replace it. Rather, I could just edit the replicated storage script and it do it for every tree in the game.
I’ve tried many different takes on this, one seemed to work with ipairs but it cloned the script into every part within the tree model. Not just the WoodcuttingPrompt within it. What are your thoughts?
The script only works for the first model named “Tree” in workspace but not the others.
local replicatedStorage = game:GetService("ReplicatedStorage")
local workspace = game:GetService("Workspace")
local woodcuttingScript = replicatedStorage.Fame:WaitForChild("Woodcutting")
for _, treeModel in ipairs(workspace:GetChildren()) do
if treeModel:IsA("Model") and treeModel.Name == "Tree" then
for _, part in ipairs(treeModel:GetDescendants()) do
if part.Name == "WoodcuttingPrompt" then
local newScript = woodcuttingScript:Clone()
newScript.Parent = part
end
end
end
end
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.
Thank you for this, this was a good starting video, however I found this video to be the most useful in my case because I am using ProximityPrompts for a majority of my game!