Touched event help

Sorry, I do not know how to title this.
So I made these fading blocks for my game, it works fine, but it is on the sever, so if someone is in front of you and steps on a fading block, it will disappear for you too.

So I used collection service to and made it on the client, this is the script:
It is a local script

local CollectionService = game:GetService("CollectionService")

local FadingParts = CollectionService:GetTagged("FadingParts")

for _, FadingPart in pairs(FadingParts) do
	local Debounce = false
	FadingPart.Touched:Connect(function()
		if Debounce == false then
			Debounce = true
			for i = 0, 1, 0.2 do
				FadingPart.Transparency = i
				wait(0.1)
			end
			FadingPart.CanCollide = false
			wait(1)
			
			for i = 1, 0, -0.2 do
				FadingPart.Transparency = i
				wait(0.1)
			end
			FadingPart.CanCollide = true
			Debounce = false
		end		
	end)
end

The script works fine, the blocks do fade, but it happens for all players.

1 Like

If it happens in a local script it shouldn’t appear for all players, are you sure this is done in a local script and not a server one?

It is done in a local script that is inside StarterCharacterScripts, is that the problem?

If that’s the case it should only appear for you, how do you know it appears for all players? It could be that you’re mistaken and it really only appears for you.

I had my friend touch the fading part, and I saw it fade too.

This is because you need to check if the part is touched by yourself. Right now, it’s detecting it being touched by all characters, so you need to have a conditional statement to check if your OWN character is touching the part. You can do this by doing this:

local character = script.Parent
local FadingParts = CollectionService:GetTagged("FadingParts")

for _, FadingPart in pairs(FadingParts) do
	local Debounce = false
	FadingPart.Touched:Connect(function(hit)
		if not hit.Parent == character then return end
		if Debounce == false then
			Debounce = true
			for i = 0, 1, 0.2 do
				FadingPart.Transparency = i
				wait(0.1)
			end
			FadingPart.CanCollide = false
			wait(1)
			
			for i = 1, 0, -0.2 do
				FadingPart.Transparency = i
				wait(0.1)
			end
			FadingPart.CanCollide = true
			Debounce = false
		end		
	end)
end
3 Likes