CollectionService on Disappearing Bricks not working

Hey all! I’m new to CollectionService, and am trying to create a script that would make any part I touch tween to be invisible and go non-collidable. Using the script given on the create.roblox website, I’ve made some small tweaks and added the tween. It seeming works, but, instead of making the bricks tween it makes my character’s legs disappear instead. I am wondering how I am able to achieve the effect I am looking for, considering that I don’t know how to make a part tween with CollectionService.

local CollectionService = game:GetService("CollectionService")

local tag = "Fall"

local function onFallPartTouched(otherPart)
	if not otherPart.Parent then
		return
	end
	
	local Info = TweenInfo.new(1)
	local Tween = game:GetService("TweenService"):Create(otherPart,Info,{Transparency=1})--Brick disappear

	local Info2 = TweenInfo.new(1)
	local Tween2 = game:GetService("TweenService"):Create(otherPart,Info2,{Transparency=0})--Brick re-appear
	
	wait(1)
	Tween:Play() --Brick disappears
	otherPart.CanCollide = false

	wait(5) -- Respawn brick timer

	otherPart.Parent.CanCollide = true
	Tween2:Play() --Brick re-appears
end

-- Save the connections so they can be disconnected when the tag is removed
-- This table maps BaseParts with the tag to their Touched connections
local connections = {}

local function onInstanceAdded(object)
	-- Remember that any tag can be applied to any object, so there's no
	-- guarantee that the object with this tag is a BasePart.
	if object:IsA("BasePart") then
		connections[object] = object.Touched:Connect(onFallPartTouched)
	end
end

local function onInstanceRemoved(object)
	-- If we made a connection on this object, disconnect it (prevent memory leaks)
	if connections[object] then
		connections[object]:Disconnect()
		connections[object] = nil
	end
end

-- Listen for this tag being applied to objects
CollectionService:GetInstanceAddedSignal(tag):Connect(onInstanceAdded)
CollectionService:GetInstanceRemovedSignal(tag):Connect(onInstanceRemoved)

-- Also detect any objects that already have the tag
for _, object in pairs(CollectionService:GetTagged(tag)) do
	onInstanceAdded(object)
end

This is what I have right now, thank you in advance!

Touched expects one param in the Connect function. The parameter refers to the part which touches the part the Touched event is assigned. The following code explains how it works:

local conn = object.Touched:Connect(function(hit)
	print(hit:GetFullName()) -- It will print the character body part's hierarchy that touches the brick.
end)

To use the brick in the function, you need to insert another function inside Connect, like this:

local tag = "Fall"

local TweenService = game:GetService("TweenService")
local CollectionService = game:GetService("CollectionService")

local function onFallPartTouched(charBodyPart, brickTouched) -- Use the brick
    if not charBodyPart.Parent then -- Check if the character body part exists
		return
	end
	
	local Info = TweenInfo.new(1)
	local Tween = TweenService:Create(brickTouched, Info, {Transparency=1})

	local Info2 = TweenInfo.new(1)
	local Tween2 = TweenService:Create(brickTouched, Info2, {Transparency=0})
	
	task.wait(1)
	brickTouched.CanCollide = false
	Tween:Play()

	task.wait(5)
	brickTouched.CanCollide = true
	Tween2:Play()
end

-- Save the connections so they can be disconnected when the tag is removed
-- This table maps BaseParts with the tag to their Touched connections
local connections = {}

local function onInstanceAdded(object)
	-- Remember that any tag can be applied to any object, so there's no
	-- guarantee that the object with this tag is a BasePart.
	if object:IsA("BasePart") then
		connections[object] = object.Touched:Connect(function(charBodyPart)
            onFallPartTouched(charBodyPart, object) -- Pass the second parameter.
        end)
	end
end

local function onInstanceRemoved(object)
	-- If we made a connection on this object, disconnect it (prevent memory leaks)
	if connections[object] then
		connections[object]:Disconnect()
		connections[object] = nil
	end
end

-- Listen for this tag being applied to objects
CollectionService:GetInstanceAddedSignal(tag):Connect(onInstanceAdded)
CollectionService:GetInstanceRemovedSignal(tag):Connect(onInstanceRemoved)

-- Also detect any objects that already have the tag
for _, object in pairs(CollectionService:GetTagged(tag)) do
	onInstanceAdded(object)
end

You may not pass the character body part in your code if redundant; however.

2 Likes

This is exactly what I needed, functions just like how I was looking for! Thank you for the explanation and solution, I’ll be sure to keep this in mind moving forward. Take care!

1 Like

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