Script still works even when not touching

I want to make it be that I can clean up a puddle when I’m standing on it. However, it still works when I step off of it.

Puddle Script:

script.Parent.Touched:Connect(function(part)
	if part.Name == 'Left Leg' then --checks if leg is touching
		RE.OnServerEvent:Connect(function() --once the event is fired, following will happen:
			if not isCleaning then
				isCleaning = true
				script.Parent.Transparency = script.Parent.Transparency + 0.2 --makes the puddle more transparent
				timer = 2
				script.Parent.CanCollide = false
				wait(1)
				isCleaning = false
			end
		end)
	end
end)

Mop Script:

local Tool = script.Parent
local character = game:GetService('Players').LocalPlayer.Character
local animation = Tool:WaitForChild('Animation')
local animationTrack = character:WaitForChild('Humanoid'):LoadAnimation(animation)
local RE = game.ReplicatedStorage:WaitForChild('MopClean')
local RESETSECONDS = 1
local isClicked = false
--8949941329

Tool.Equipped:Connect(function(mouse)
	mouse.Button1Down:Connect(function()
		if not isClicked then
			RE:FireServer() --fires event once clicked
			animationTrack:Play()
			isClicked = true
			wait(RESETSECONDS)
			isClicked = false
		end
	end)
end)

Thanks for any help!

The OnServerEvent listener is still active even after the touch has ended. You should add a TouchEnded listener that disconnects the event listener. Let me know if you need further explanation.

1 Like

In your puddle script, you’re creating a new connection to the RemoteEvent every single time the puddle is touched by a Left Leg.
You’ll want to either use .Touched and .TouchEnded to keep track of whether or not a left leg is currently touching the puddle, or better yet, create a single connection to the RemoteEvent and use :GetTouchingParts whenever it is fired.
Here’s what that may look like:

local puddle = script.Parent
RE.OnServerEvent:Connect(function(player)
    for _, part in ipairs(puddle:GetTouchingParts()) do
        if part.Parent == player.Character then
            -- your code for cleaning or whatever
        end
    end
end)

There’s another issue I’m seeing with your tool script, you should only connect to the Button1Down event once, you’re creating a new connection every single time the tool is equipped which will lead to the event being fired several times at once. You should use .Equipped and .Unequipped to keep track of the current state of the tool and listen for the mouse click elsewhere. In your situation, it would probably be better to listen to Tool.Activated, anyways.

1 Like