Collection Service in a local script

Hello!
I was wondering if anyone is familiar with the collection service and help me out with this bug. I am trying to create a part where if a player touches it, it slowly falls down using tween service. I wrote the code in a local script so it only happens for that player, but then I came a cross a bug where it tweens for other players as well even if they are not touching it. I looked at the server view and it’s not tweeting there; just other clients.

Your help is much appreciated!

Code:

local TweenService = game:GetService("TweenService")
local CollectionService = game:GetService("CollectionService")
local GetParts = CollectionService:GetTagged("Falling")

for _, v in pairs(GetParts) do
	local TweenInfo1 = TweenInfo.new(
		v:GetAttribute("Duration"),	 --3						
		Enum.EasingStyle.Linear,		
		Enum.EasingDirection.InOut,		
		0,								
		false,							
		0							
	)
	
	local TweenGoals1 = {
		Position =  v.Position + Vector3.new(0,-5,0),
		Transparency = 1
	}
	
	local TweenGoals2 = {
		Transparency = 1
	}

	local Tween1 = TweenService:Create(v,TweenInfo1,TweenGoals1)
	local Tween2 = TweenService:Create(v.Decal,TweenInfo1,TweenGoals2)

	v.Touched:Connect(function(hit)
		local Humanoid = hit.Parent:FindFirstChild("Humanoid")
		if Humanoid  then
			coroutine.resume(coroutine.create(function()
				local OrigPos = v.Position
				Tween1:Play()
				Tween2:Play()
				Tween1.Completed:Connect(function()
					v.CanCollide = false
					wait(v:GetAttribute("Duration"))
					v.CanCollide = true
					v.Position = OrigPos
					v.Transparency = 0
					v.Decal.Transparency = 0.5
				end)
			end))
		end
	end)	
end

I don’t see the part where it detects when the player collides with the part…

My bad. I copied and pasted the wrong script. I just fixed it.

1 Like

The .Touched event is the problem. You aren’t checking who touched it.
(also there’s no point of using a coroutine in this case)

local player = game.Players.LocalPlayer
v.Touched:Connect(function(hit)
    local Humanoid:Humanoid = hit.Parent:FindFirstChild("Humanoid")
    if not Humanoid then return end
    
    local toucher = game.Players:GetPlayerFromCharacter(Humanoid.Parent)
    if not toucher then return end
    if toucher ~= player then return end
    
    local OrigPos = v.Position
    Tween1:Play()
    Tween2:Play()
    Tween1.Completed:Connect(function()
        v.CanCollide = false
        task.wait(v:GetAttribute("Duration"))
        v.CanCollide = true
        v.Position = OrigPos
        v.Transparency = 0
        v.Decal.Transparency = 0.5
        
        Tween1:Destroy()
    end)
end)	
1 Like
if toucher ~= player then return end

by “player”, do you mean “toucher” because “player” is undefined?
like

if toucher ~= toucher then return end

Look at the top of the script, “player” is a variable.

Thank you so much for the quick help! It all worked how I wanted it.