I made a forest and when I calculated how many trees I had I got 110.
The problem is that I forgot that I needed to put scripts in every tree, but I have no idea how or if you can quickly put a script in a large amount of parts.
I don’t want to have to rebuild it all so I’m looking to see if anyone has any ideas on how to put a script in each tree quickly.
I’m trying to do an effect where after one tree is triggered to glitch the rest trigger to glitch one after the other and at the end it should kinda go like this:
When you touch a part a tree starts glitching. Half a second later another one starts glitching. Another second later another tree glitches, following like that until eventually each tree disappears entirely and the forest just glitches out of existence.
I mean it’d be great if someone knew how to get that all in one script but I was just planning on using a module script to shorten the size of each script since I had done some tests before and know how to do it.
Well, its very simple actually, all you need to do is create a loop of workspace or whatever folder the trees are in, check every object to see if it named tree, if it is clone a script into it. That script, on activation would glitch the tree its in, then activate another tree’s script and after its done delete itself.
-- This should work, if it doesn't flip idex and descendant around
for index, Descendant in pairs(workspace) do
if Descendant.Name == "tree" then
local Clone = game.ServerStorage.Script:Clone()
Clone.Parent = Descendant
end
end
You should use loops, but not in each tree, if you put that many scripts your game may lag, try finding a solution to cause this glitch effect in a script that has a loop, instead of using a loop to copy paste all the scripts into that many trees. It is very possible to do with one script.
Each tree needs to be triggered by another tree. @TheLegendaryEggMan solved that problem but there’s where I have my second problem
Each tree is triggered by a specific tree. This way it creates a wave or domino effect where you have the tree with a part that triggers it then the rest follow one after the other. With @TheLegendaryEggMan his solution would make the tree affected random and could actually select a tree that was already affected. Granted you could fix that by adding a name change into the function.
Kind of like this
Tree 1 (Named Tree1):
local Trigger = game.Workspace.Trigger
local Tree = script.Parent
Trigger.Touched:Connect(function(otherpart)
local Humanoid = otherpart.Parent:FindFirstChild("HumanoidRootPart")
if Humanoid then
for i = 1, 20 do
task.wait(math.random(.8,1)
Tree.Transparency = 1
task.wait(math.random(.1,.3)
Tree.Transparency = 0
end
Tree.Transparency = 1
Tree.CanCollide = false
end
end)
Tree 2 (Named Tree2):
local Tree1 = game.Workspace.Tree1
local Tree2 = script.Parent
if Tree1.Transparency == 1 then
for i = 1, 20 do
task.wait(math.random(.8,1)
Tree.Transparency = 1
task.wait(math.random(.1,.3)
Tree.Transparency = 0
end
Tree.Transparency = 1
Tree.CanCollide = false
end
Say there was a Tree3. It would have the same script as Tree2 but the variable Tree1 would have game.Workspace.Tree2 instead of game.Workspace.Tree1 because Tree3 would be triggered by Tree2
Wrote a quick example of a possible solution, I won’t explain it since I’m really bad at explaining stuff. You’ll have to put all of your trees inside a folder (named TreesFolder in the example below)
local trees = workspace.TreesFolder
local treeTable = {}
for index, tree in pairs (trees:GetChildren()) do
table.insert(treeTable, tree)
for i = 1, 20 do
treeTable[i].Transparency = 1
task.wait(math.random(.8,1)
end
end
end