How to adjust this script to spawn multiple blocks/parts for "Falling Block" minigame?

Howdy, i’m making a “Falling Block” minigame & i’ve gotten stuck with it. I want to achieve so the script spawns blocks every couple of seconds, but my code isn’t optimal for this, because it has a “repeat loop” in it. How could i remove this loop, or just adjust it to spawn blocks every couple of seconds without making the code very messy and long? Help & feedback is very much appreciated!

here’s a video of the code in action:

here’s the code:

local fallingPart = game.ReplicatedStorage.FallingPart
	
	local platforms = game.Workspace.ActivePlatforms:GetChildren()

	local clone = fallingPart:Clone()
	clone.Parent = workspace

	local randomPlatform = platforms[math.random(1,#platforms)]
	clone.Position = Vector3.new(randomPlatform.Position.X, 50, randomPlatform.Position.Z)
	repeat wait(0.01)

		if randomPlatform:FindFirstChild("Shadow").Transparency >= 0 then

			randomPlatform:FindFirstChild("Shadow").Transparency = randomPlatform:FindFirstChild("Shadow").Transparency - 0.01

		end

		clone.Position = Vector3.new(clone.Position.X,clone.Position.Y - 0.5, clone.Position.Z)

	until clone.Position == Vector3.new(randomPlatform.Position.X, randomPlatform.Position.Y + 3, randomPlatform.Position.Z)

	clone.Name = "Platform"
	clone.Parent = workspace.ActivePlatforms
	randomPlatform.Parent = workspace.UnactivePlatforms

2 Likes

First of all I personally wouldn’t use loops for this type of thing and would use tweens instead. Then make a function to store the loops so you can call the function multiple times. Something like:

local fallingPart = game.ReplicatedStorage.FallingPart

local tweenService = game:GetService("TweenService")

local tweenInfo = TweenInfo.new(--[[Enter tween info here]])
	
local platforms = game.Workspace.ActivePlatforms:GetChildren()

-- Call this whenever you need new part
local function cloneAPart()
	local clone = fallingPart:Clone()
	clone.Parent = workspace
	
	local randomPlatform = platforms[math.random(1, #platforms)]
	clone.Position = Vector3.new(randomPlatform.Position.X, randomPlatform.Position.Y + 50, randomPlatform.Position.Z)
	endPosition = Vector3.new(randomPlatform.Position.X, randomPlatform.Position.Y + 3, randomPlatform.Position.Z)
	endShadowAmount = 0
	
	local tween = TweenService:Create(clone, tweenInfo, {endPosition, randomPlatform:FindFirstChild("Shadow").Transparency = 0})

	tween:Play()

	clone.Name = "Platform"
	clone.Parent = workspace.ActivePlatforms
	randomPlatform.Parent = workspace.UnactivePlatforms
end)

*Untested

1 Like

Thank You, I had to do a bit of bugtesting, but everything seems to be working now. Appreciate the help!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.