For i, v only works on 2 or less descendants

this is a continuation of my previous question but with a different question, so what im trying to do is to call descendants the have the tag “Grow” which is all inside a Prompt and do stuff with it, but there is another script which find if the descendants of “Plants” are a union operation
(There are two scripts, a Local script inside a tool and a regular one inside server script)

it works for two or one of the plants there (for example back and starter) but it completely broke for the others, no error on the output
i believe it’s called yielding? not super familiar with it so i had no idea how to fix it

the original set of scripts

Server Script
local plants = workspace.Plants
local tree = game:GetService("ReplicatedStorage").Tree

local event = game.ReplicatedStorage.Event:WaitForChild("Tree")
local ts = game:GetService("TweenService")
local goal = {}
goal.Transparency = 0.5
goal.Size = Vector3.new(30, 30, 30)

for i, v in ipairs(plants:GetDescendants()) do
	if v:IsA("UnionOperation") then
		local sz = v.Safezone
		local dirtpos = v:GetAttribute("Pivot")
		
		local tinfo = TweenInfo.new(5)
		local tween = ts:Create(sz, tinfo, goal)

		sz.Transparency = 1
		v.Prompt.Triggered:Connect(function()
			v.Prompt:Destroy()
			v.Tree:Destroy()
			tween:Play()
			local cloned = tree:Clone()
			cloned.Parent = v.Parent.Trees
			cloned:PivotTo(dirtpos)
		end)
	end
end
Local Script
local plants = workspace.Plants
local water = script.Parent.Water.Particle

local currentnum = game.Players.TotalPlant
local event = game.ReplicatedStorage.Event:WaitForChild("Tree")

water.Enabled = false

for i, v in ipairs(plants:GetDescendants()) do
	if v:HasTag("Grow") then
		v.Enabled = false

		script.Parent.Equipped:Connect(function()
			v.Enabled = true
			v.Triggered:Connect(function()
				event:Fire()
				v:Destroy()
				water.Enabled = true
				wait(3)
				currentnum.Value += math.clamp(1,1,1)
				water.Enabled = false
			end)
		end)
		script.Parent.Unequipped:Connect(function()
			v.Enabled = false
		end)
	end
end

btw the math clamp is my attempt to make the value not doubled, it works fairly ok lol

my attempt of using an event (does not work)

Server Script
local plants = workspace.Plants
local tree = game:GetService("ReplicatedStorage").Tree

local event = game.ReplicatedStorage.Event:WaitForChild("Tree")

local ts = game:GetService("TweenService")
local tinfo = TweenInfo.new(5)
local goal = {}
goal.Transparency = 0.5
goal.Size = Vector3.new(30, 30, 30)


for i, v in ipairs(plants:GetDescendants()) do
	if v:IsA("UnionOperation") then
		local sz = v.Safezone
		local dirtpos = v:GetAttribute("Pivot")
		
		local tween = ts:Create(sz, tinfo, goal)
		
		sz.Transparency = 1
		local function cool()
			v.Prompt:Destroy()
			v.Tree:Destroy()
			tween:Play()
			local cloned = tree:Clone()
			cloned.Parent = v.Parent.Trees
			cloned:PivotTo(dirtpos)
		end
		event.Event:Connect(cool)
	end
end
Local Script
local plants = workspace.Plants
local water = script.Parent.Water.Particle

local currentnum = game.Players.TotalPlant
local event = game.ReplicatedStorage.Event:WaitForChild("Tree")

water.Enabled = false

for i, v in ipairs(plants:GetDescendants()) do
	if v:HasTag("Grow") then
		v.Enabled = false

		script.Parent.Equipped:Connect(function()
			v.Enabled = true
			v.Triggered:Connect(function()
				event:Fire()
				v:Destroy()
				water.Enabled = true
				wait(3)
				currentnum.Value += math.clamp(1,1,1)
				water.Enabled = false
			end)
		end)
		script.Parent.Unequipped:Connect(function()
			v.Enabled = false
		end)
	end
end

here is my hierarchy, the unions all had the same childrens
again, Grow tag is inside Prompt
Screenshot_161

so yeah, i would appreciate any help like always im kinda stuck here lol

edit :
i provided a simpler version of the game’s file for testing here

3 Likes

Can we get further elaboration on what these lines mean and what’s going wrong?

1 Like

You can’t fire a bindable event that is used by from the client, by the way. That is what a RemoteEvent is for.

1 Like

there are two script in which i suspect could be conflicting with each other, one is inside the server script storage and the other is inside a tool in starter pack

the server script one gets the parent of the prompt and wait till it get activated then clones a tree

the local script is for getting the prompt inside the parent to make the prompt visible if the tool is equipped, and then fire the event and adding the value for the player

No, scripts in ServerStorage do not run.

Do you mind specifying what exactly is wrong? Potentially consider sending a video?

huh, what’s the difference between the two…?

it’s on server script service not server storage, my problem is that the script manage to clone the tree two times and then failed on other Plants children while there is no different between all of them except position in the game

I agree with @yungpharma . Please send a video, as from your explanation we have no idea what the problem is

1 Like

ok so after getting 3 points for watering the plants notice how the 4th “forcefield” pop meaning it works as intended but the points did not rise up? even though it is a direct duplicate of the first working one

some are the trees does not pop up, thats normal because i havent set up the position correctly

oh and the top of the world one isnt the one i just watered, it’s just a display which one “works” and which one isn’t being watered (tree and prompt available = hasnt been watered, only safezone available = it worked)

I misunderstood, my mistake. Maybe try to use GetChildren instead? In luau, You don’t even have to use ipairs, pairs or next anymore. It’s built in.

for i, v in plants:GetChildren() do
	if v:IsA("UnionOperation") then
		local sz = v:FindFirstChild("Safezone")
		if not sz then warn("no safezone found") continue end
		local dirtpos = v:GetAttribute("Pivot")
		
		local tween = ts:Create(sz, tinfo, goal)
		
		sz.Transparency = 1
		local function cool()
			v.Prompt:Destroy()
			v.Tree:Destroy()
			tween:Play()
			local cloned = tree:Clone()
			cloned.Parent = v.Parent.Trees
			cloned:PivotTo(dirtpos)
		end
		event.Event:Connect(cool)
	end
end

I’d recommend checking the output for potential errors as well.

oh dont sweat it. but this code does not work sadly, i think your previous comment about bindable seems to be true
(i will probably learn about remote event tomorrow)

weirdly by moving currentnum.Value += math.clamp(1,1,1) to the Script on Server Script Service solved the value increasing problem… but in the cost of breaking the prompt visibility. what?!?

previously, by unequipping the tool the prompt indeed hides but now its just visible… is there a problem on the local script maybe?

the codes are similar to this except for the currentnum switching places

I dont see any YieldFunctions directly in the loop? The only one I’ve seen is wait() and thats in an event handler (Connect doesn’t yield).

For reference, a yielding function is one that pauses the execution of that script and it waits for something to happen, for example, wait (or task.wait ← use this its better), yields the script for a defined amount of time.

I find it possible that the descendants have not yet finished loading into the game.

Here is an updated version of the code that I believe may work. (UNTESTED).

Try changing this to your original server script’s code.

local plants = workspace.Plants
local tree = game:GetService("ReplicatedStorage").Tree

local event = game.ReplicatedStorage.Event:WaitForChild("Tree")
local ts = game:GetService("TweenService")
local goal = {}
goal.Transparency = 0.5
goal.Size = Vector3.new(30, 30, 30)

plants.DescendantAdded:Connect(function(v) -- You can actually just change this to ChildAdded if you wish, from what I've seen in the set up it doesn't look entirely necessary to loop the descendants. Your call!
	if v:IsA("UnionOperation") then
		local sz = v.Safezone
		local dirtpos = v:GetAttribute("Pivot")
		
		local tinfo = TweenInfo.new(5)
		local tween = ts:Create(sz, tinfo, goal)

		sz.Transparency = 1
		v.Prompt.Triggered:Connect(function()
			v.Prompt:Destroy()
			v.Tree:Destroy()
			tween:Play()
			local cloned = tree:Clone()
			cloned.Parent = v.Parent.Trees
			cloned:PivotTo(dirtpos)
		end)
	end
end)

Hope this helps, let me know.

i see some improvement from just picking up 2 descendants it rises up to 4-5! although my total plants are 6… ill be changing the for loops then, seems more reliable

and for testing sake i provided this (slightly) simpler version of the map, the code had a slight edit but nothing major

Ill add on to what Pilot’s reply was:

You should also do an initial check through any plants that already exist, instead of just when new ones are loaded:

local plants = workspace.Plants
local tree = game:GetService("ReplicatedStorage").Tree
local event = game.ReplicatedStorage.Event:WaitForChild("Tree")
local ts = game:GetService("TweenService")
local goal = {}
goal.Transparency = 0.5
goal.Size = Vector3.new(30, 30, 30)

local function processPlant(v)
	if v:IsA("UnionOperation") then
		local sz = v.Safezone
		local dirtpos = v:GetAttribute("Pivot")
		
		local tinfo = TweenInfo.new(5)
		local tween = ts:Create(sz, tinfo, goal)
		sz.Transparency = 1
		v.Prompt.Triggered:Connect(function()
			v.Prompt:Destroy()
			v.Tree:Destroy()
			tween:Play()
			local cloned = tree:Clone()
			cloned.Parent = v.Parent.Trees
			cloned:PivotTo(dirtpos)
		end)
	end
end
plants.DescendantAdded:Connect(processPlant)
for _, plant in ipairs(plants:GetChildren()) do 
    processPlant(plant)
end
1 Like

In theory it should run fast enough, but yeah it wouldn’t hurt to initially run through all of them, then do the check for any more added. I feel like this one here is going to be the correct solution.

it worked on Server Script! but no luck for Local Script… it manage to get 6 once but overall only 4 fully gone through.
here’s the script so far

Local Script (in Tool)
local plants = workspace.Plants
local water = script.Parent.Water.Particle

local event = game.ReplicatedStorage.Event:WaitForChild("Tree")

water.Enabled = false

local function processPlant(v)
	if v:HasTag("Grow") then
		print(v.Name.. " is inside  '" ..v.Parent.Name.. "'")
		v.Enabled = false

		script.Parent.Equipped:Connect(function()
			v.Enabled = true
			v.Triggered:Connect(function()
				v:Destroy()
				event:FireServer()
				water.Enabled = true
				
				task.wait(3)
				water.Enabled = false
			end)
		end)
		script.Parent.Unequipped:Connect(function()
			v.Enabled = false
		end)
	end
end
plants.DescendantAdded:Connect(processPlant)
for _, plant in ipairs(plants:GetChildren()) do 
	processPlant(plant)
end
Server Script Service
local plants = workspace.Plants
local tree = game:GetService("ReplicatedStorage").Tree

local event = game.ReplicatedStorage.Event:WaitForChild("Tree")
local currentnum = game.Players.TotalPlant

local ts = game:GetService("TweenService")
local goal = {}
goal.Transparency = 0.5
goal.Size = Vector3.new(30, 30, 30)


local function processGrowth(v)
	if v:IsA("UnionOperation") then
		local sz = v.Safezone
		local dirtpos = v:GetAttribute("Pivot")

		local tinfo = TweenInfo.new(5)
		local tween = ts:Create(sz, tinfo, goal)
		sz.Transparency = 1
		
		v.Prompt.Triggered:Connect(function()
			v.Prompt:Destroy()
			v.Tree:Destroy()
			tween:Play()
			currentnum.Value += 1
			
			local cloned = tree:Clone()
			cloned.Parent = v.Parent.Trees
			cloned:PivotTo(dirtpos)
		end)
	end
end
plants.DescendantAdded:Connect(processGrowth)
for _, plant in ipairs(plants:GetChildren()) do 
	processGrowth(plant)
end

is this a lost cause? i might just abandoned the prompt hiding for simplicity sake

Is streaming enabled on currently? If so, it may change the behaviour a bit.

yup, and turning it off doesn’t work at all

For client, only close stuff to the player will load and be detected.

woah! it fully worked now! i just changed :GetChildren to :GetDescendants in the Local and it now it gets all 6! Thank you so much!!
but i do wonder, why is my previous attempts only manage to get 4 or less? it manage to get all six on server but not locally