Made a CS (Collection Service) way of handling everything in my obby, any improvements?

local killbrick = script.Parent

local CS = game:GetService("CollectionService")

local TS = game:GetService("TweenService")




for _, TaggedParts in pairs(CS:GetTagged("KillBricks")) do
	TaggedParts.Touched:Connect(function(hit)
		if hit and hit.Parent:FindFirstChild("Humanoid") then
			hit.Parent:FindFirstChild("Humanoid"):TakeDamage(100)
		end
	end)
end

for _, TaggedParts in pairs(CS:GetTagged("RotatingKillBricks")) do
	local TweenInfo1 = TweenInfo.new(
		1.5,							
		Enum.EasingStyle.Linear,		
		Enum.EasingDirection.InOut,		
		-1,								
		false,							
		0							
	)

	local TweenGoals1 = {
		Orientation = TaggedParts.Orientation + Vector3.new(0,0,360);
	}

	local Tween1 = TS:Create(TaggedParts,TweenInfo1,TweenGoals1)

	Tween1:Play()
end

for _, TaggedParts in pairs(CS:GetTagged("SlowRotatingKillBricks")) do
	local TweenInfo1 = TweenInfo.new(
		3,							
		Enum.EasingStyle.Linear,		
		Enum.EasingDirection.InOut,		
		-1,								
		false,							
		0								
	)

	local TweenGoals1 = {
		Orientation = TaggedParts.Orientation + Vector3.new(0,0,360);
	}

	local Tween1 = TS:Create(TaggedParts,TweenInfo1,TweenGoals1)

	Tween1:Play()
end

for _, TaggedParts in pairs(CS:GetTagged("FadingPlates")) do
	local TweenInfo2 = TweenInfo.new(5, Enum.EasingStyle.Back, Enum.EasingDirection.InOut, -1, true, 0)
	
	local TweenGoals = {
		Transparency = 1
	}
	
	local Tween1 = TS:Create(TaggedParts, TweenInfo2, TweenGoals)
	
	Tween1:Play()
end

for _, TaggedParts in pairs(CS:GetTagged("FallingBricks"))  do
	
	
	
	local TweenInfo3 = TweenInfo.new(1.5, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out, 0, true, 1)

	local TweenGoals2 = {
		Transparency = 1,
		Position = Vector3.new(TaggedParts.Position.X, TaggedParts.Position.Y - 15.867, TaggedParts.Position.Z),
		CanCollide = false
	}

	local Tween2 = TS:Create(TaggedParts, TweenInfo3, TweenGoals2)
	
	local function onPlaybackChanged(PS)
		PS = Tween2.PlaybackState
		
		if PS == Enum.PlaybackState.Delayed  then
			print("waiting")
			TaggedParts.CanCollide = true
		elseif PS == Enum.PlaybackState.Playing then
			print("lol")
			TaggedParts.CanCollide = false	
		elseif PS == Enum.PlaybackState.Completed then
			print("done")
			TaggedParts.CanCollide = true
		end
	end
	
	local TweenProp = Tween2:GetPropertyChangedSignal("PlaybackState")

	TaggedParts.Touched:Connect(function(hit)
		
		if hit and hit.Parent:FindFirstChild("Humanoid") then
			
			TweenProp:Connect(onPlaybackChanged)
			Tween2:Play()

			
				
				
		end
	end)
end

for _, TaggedParts in pairs(CS:GetTagged("MovingPlatesHor"))  do
	
	
	local Weight = 0
	
	local Chances = {MovingUP = 0.50, MovingDOWN = 0.50}
	
	local TweenInfo1, TweenInfo2 =  TweenInfo.new(2, Enum.EasingStyle.Bounce, Enum.EasingDirection.Out, 0, true, 0), TweenInfo.new(2, Enum.EasingStyle.Bounce, Enum.EasingDirection.In, 0, true, 0)
	
	local TweenGoalsUP, TweenGoalsDW = {CFrame = TaggedParts.CFrame * CFrame.new(0, 10, 0)}, {CFrame = TaggedParts.CFrame * CFrame.new(0, -10, 0)}
	
	local MovingUP, MovingDOWN = TS:Create(TaggedParts, TweenInfo1, TweenGoalsUP), TS:Create(TaggedParts, TweenInfo2, TweenGoalsDW)
	
	local Tween1UP = MovingUP:GetPropertyChangedSignal("PlaybackState")
	local Tween1DOWN = MovingDOWN:GetPropertyChangedSignal("PlaybackState")
	
	local function MUPDone(PS)
		PS = MovingUP.PlaybackState
		if MovingUP.PlaybackState == Enum.PlaybackState.Completed then
			MovingDOWN:Play()
		end
	end
	
	local function MDOWNDone(PS)
		PS = MovingDOWN.PlaybackState
		if MovingDOWN.PlaybackState == Enum.PlaybackState.Completed then
			MovingUP:Play()
		end
	end
	
	local function Randomization()
		for _, Chance in pairs(Chances) do
			--	print("Rolling")
			Weight += (Chance * 10)
		
		end
		local ranNumber = math.random(1, Weight)
		--print("Got it")
		Weight = 0
		for Tween, Chance in pairs(Chances) do
			--print("Just debugging")
			Weight += (Chance * 10)
			
			--print("Still bugging")
			if Weight >= ranNumber then
				--print("You got a tween itsa!".. Tween)
				return Tween
				
			end
		end 	
	end
	local Tween = Randomization()
	
	if Tween == "MovingUP" then
		--print("Please dude")
		Tween1UP:Connect(MUPDone)
		Tween1DOWN:Connect(MDOWNDone)
		MovingUP:Play()
		
	elseif Tween == "MovingDOWN" then
		Tween1DOWN:Connect(MDOWNDone)
		Tween1UP:Connect(MUPDone)
		MovingDOWN:Play()
	
	end
end

for _, TaggedParts in pairs(CS:GetTagged("SpinningBricks"))  do
	
	
	
	local TI1 = TweenInfo.new(2, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut, -1, false)
	
	local itsareverse = TS:Create(TaggedParts, TI1, {Orientation = TaggedParts.Orientation + Vector3.new(0, 180, 0)})
	
	itsareverse:Play()
	
end

for _, TaggedParts in pairs(CS:GetTagged("Conveyor")) do
	
	while true do 
		TaggedParts.Velocity = TaggedParts.CFrame.lookVector * 15
		wait(0.1)
	end	
	
	
end

I use collection service to handle most of my obby’s work when it comes to hazards and things of that nature, and I was just curious if this was a good method to use when it comes to dealing with something such as this.

I think this is a good idea, the main questions are:

  1. Can you easily add new obstacles / stages?
  2. Is your code readable?

It seems like the code is pretty readable, so I think you have a good base, although I would recommend moving function definition out of loops, and I like to put all functions at the top of the script.

1 Like