Firing moving script on multiple parts independent of eachother

This is an obstacle course. Put simply, I have a bunch of tagged platforms one after another that are supposed to sink (then rise again) when a player jumps on them. Originally, I had identical scripts in each part that did the same thing: sink on touch, rise after a delay. To optimize this, I tried setting up a main script and called upon every platform using the CollectionService.

This issue is, due to the debounce and the nature of the CollectionService, each part will sink on touch only
after the first part has completed its sink/rise. So, rather than jumping from platform to platform, narrowly avoiding death, you can just jump past the first sinking platform and cruise on the following stationary platforms ahead while the first one completes its movement.

I’ve tried thinking of more conditions that could possibly solve this issue, but I can’t think of any. Removing the debounce causes the platforms to continuously fall on touch, with each consecutive platform falling faster.

local CollectionService = game:GetService("CollectionService")

local SinkingBricks = CollectionService:GetTagged("SinkingBricks")

local stationary = true 
local moving = false

for _, sinks in pairs(SinkingBricks) do
	sinks.Touched:connect(function(hit)
		if hit and hit.Parent:FindFirstChild("Humanoid") and stationary == true then
			stationary = false
			for i = 1, 50 do -- Sinking
				sinks.CFrame -= Vector3.new(0,0.4,0)
				wait()
			end
			wait(4) -- Delay before rising again
			moving = true 
			sendBack(sinks)
		return end
	end)
end

function sendBack(sinks) 
	if moving == true then 
		for i = 1, 50 do -- Rising
			sinks.CFrame += Vector3.new(0,0.4,0)
			wait()
		end
	end
	stationary = true 
end

moving = false

P.S. In my infinite wisdom, I deleted the original scripts, thinking this one would work flawlessly, first try.

You can use TaskScheduler or coroutines to create multiple, separate threads running simultaneously. I forget the difference off the top if my head but I recommend reading up on it.

1 Like

Sounds like this will likely lead me to what I want to achieve, will get back to this once I’ve figured something out. Thanks for the tip!