Random Part Generation Help

Issue is - how would I make a part spawn on a part that spawned just prior to it?
I have a system where the green blocks spawn first, and then the yellow blocks are supposed to spawn right after, and hopefully in the newest spawned block.

Currently, it will spawn a single yellow before breaking the script.
I have tried using shared variables, along with a naming system that changes names after a piece is spawned. However, when I do this, it keeps spawning on only one block, or the times are off such so the script breaks.

I have 2 primary scripts and another script detecting the blocks.
Variables :
The NSVR is the green blocks, just rain. Instance A’s name changes to this after B is supposedly spawned.
The SVR is yellow, moderate rain. Spawned in as instance B.
“john” is a nickname when A is having B spawned on it. A spawns in as this, to get the newest spawned block to place B on.

ALSO,
How could I get the size of B to correspondent with A but to a half size of it? Thanks!

SCRIPT A

local nsvr = 0

--local svr = 100
repeat
	nsvr += 2
	local a = Instance.new("Part",workspace)
	a.Position = Vector3.new(math.random(-200,-100),0.1,math.random(-200,-100))
	a.BrickColor = BrickColor.new("Forest green")
	a.Name = "john"
	a.Anchored = true
	a.Material = ("SmoothPlastic")
	a.Size = Vector3.new(math.random(15,20), 0.1, math.random(15,20))
	a.Name = nsvr
wait (1)
a.Name = nsvr
	until nsvr == 100

SCRIPT B

repeat
local svr = 100
local nsvr = shared["joemama"]
	wait (0.2)
	local b = Instance.new("Part",workspace)
	b.Position = Vector3.new(nsvr.position.X, nsvr.position.Y, nsvr.position.Z )
	b.BrickColor = BrickColor.new("Deep orange")
	b.Name = svr 
	b.Anchored = true
	b.Material = ("SmoothPlastic")
	b.Size = Vector3.new(math.random(3,5), 0.15, math.random(3,5))
	svr += 1
until svr == 150 

Thanks!

It breaks in what way? Sorry I’m having a hard time understanding what the issue you’re having is.

The script terminates.
To be honest, I sorted out some of the kinks, but I really need a way to find when a part
A. spawns in
B. renames to the right name

First of all, wouldn’t it be a more practical thing if you used only one script?

if so your problem would be solved since you can change a variable

such as:

local variable2 
local variable

variable = --previose position
variable2 = --previose size devided by 2

so once A spawns, you can set A’s start position to “variable”
same thing with halving the size. Set the size to variable2 then/2

I’d recommend on just merging both scripts into one, and have after script A finishes spawning the brick, run script b.

Example:

-- the loop, but more readable (1-50 instead of 0+2/100, it's the same.)
for i = 1, 50 do
	-- script A
	local a = Instance.new("Part",workspace)
	a.Position = Vector3.new(math.random(-200,-100),0.1,math.random(-200,-100))
	a.BrickColor = BrickColor.new("Forest green")
	a.Name = "john"
	a.Anchored = true
	a.Material = Enum.Material.SmoothPlastic
	a.Size = Vector3.new(math.random(15,20), 0.1, math.random(15,20))
	a.Name = i
	
	-- Script B
	local b = Instance.new("Part",workspace)
	b.Position = Vector3.new(nsvr.position.X, nsvr.position.Y, nsvr.position.Z )
	b.BrickColor = BrickColor.new("Deep orange")
	b.Name = i
	b.Anchored = true
	b.Material = Enum.Material.SmoothPlastic
	b.Size = a.Size / 2 -- We divide the size of a into 2, obtaining it's one half.
	wait(0.2)
end

Enum.Material.SmoothPlastic
Vector3 / number

But if you want to do that, you can listen to workspace’s ChildAdded function, then see if it’s the part you want (though I don’t recommend this at all)

I too recommend merging the scripts, but if you insist on having seperate scripts, in addition to the ChildAdded event, you could use the _G table (or the shared table, which has the same purpose) or a BindableEvent to trigger a function in your second script when A is added.

Let me know if you need examples for either method.

Thanks!
I forgot to end this thread, and I figured out a way to do so.
I plan on merging the scripts, but for now, here’s the code along with a demo
My plan is so that not every part has two additives.

Here’s the scripts

--local svr = 100
shared["SVRS"] = 0
repeat
	NSVRvar += 1
	local a = Instance.new("Part",workspace)
	a.Position = Vector3.new(math.random(0,75),0.3,math.random(0,75))
	a.BrickColor = BrickColor.new("Forest green")
	a.Name = "NSVRS"
	shared["SVRS"] = 1
	print ("non-svr spawned")
	a.Anchored = true
	a.Material = ("SmoothPlastic")
	a.Size = Vector3.new(math.random(11,15), 0.1, math.random(11,15))
	wait (1)
	shared["SVRS"] = 0
	print ("non-svr renamed")
	a.Name = "NSVR"
until NSVRvar == 100
if NSVRvar == 100 then

end

Script B

shared["MODE"] = 0
while true do
	if shared["SVRS"] == 1 then
		
		local b = Instance.new("Part",workspace)
		b.Position = Vector3.new(workspace.NSVRS.Position.X,workspace.NSVRS.Position.Y,workspace.NSVRS.Position.Z)
		b.BrickColor = BrickColor.new("New Yeller")
		b.Name = "severe"
		shared["MODE"] = 1
		print ("svr spawned")
		b.Anchored = true
		b.Material = ("SmoothPlastic")
		b.Size = Vector3.new(math.random(8,10), 0.2, math.random(8,10))
		wait (0.1)
		shared["MODE"] = 0
		print ("svr renamed")
		b.Name = "tortoise"
	end
	wait (2.5)
end

and Script C

	if shared["MODE"] == 1 then
		local c = Instance.new("Part",workspace)
		c.Position = Vector3.new(workspace.severe.Position.X,workspace.severe.Position.Y,workspace.severe.Position.Z)
		c.BrickColor = BrickColor.new(Color3.new(0.882765, 0.467521, 0.0918898))
		c.Name = "MODER"
		print ("MODE spawned")
		c.Anchored = true
		c.Material = ("SmoothPlastic")
		c.Size = Vector3.new(math.random(5,7.5), 0.25, math.random(5,7.5))
		wait (0.5)
		print ("MODERATE spawned")
		c.Name = "MODERATE"
	end
	wait (2)
end

I’m still finishing Script C for a final Script D.
Cheers!