Any other ways to check if a player is touching a part instead of using .Touched?

I’m currently making a script where you can mop up spills, I have everything working but the TouchEnded fires when the player is actually touching the part. I’m looking for a different way of using TouchEnded as it’s very buggy. if you have any alternatives make sure to post

Heres my code

You can check if two parts are still touching using BasePart:GetTouchingParts()

Well, the part will be on the ground, so it will keep picking up that its on the ground

You could use magnitude to check if they are close or not.
Read this article if you want to know more about magnitude :

Will this work if the tool is touching the part?

Yeah because magnitude is checking how close is the part so if you set it very precisely you could.
The problem is that it might not work if you touch a corner and just if the mop is closer to the center

I’m a bit confused because how would I define the mop?

GetTouchingParts() returns a table of all the parts touching the objects, meaning you can cycle through all said parts to see if you can find the mop.

Example of implementing this into your code:

local spill = script.Parent
local TweenService = game:GetService("TweenService")
local RunService = game:GetService('RunService')
 
local partChanges = {
    Size = Vector3.new(0.07, 1.15, 1.15),
}
 
local tweenInfo = TweenInfo.new (
    3,
    Enum.EasingStyle.Quad,
    Enum.EasingDirection.InOut,
    0,
    false,
    0.1
   
)
 
local tween = TweenService:Create(spill, tweenInfo, partChanges)
 
script.Parent.Activated:Connect(onActivation)
 
local function checkIfTouchingMop()
    local touching = spill:GetTouchingParts()
    for i = 1,#touching do
        local part = touching[i]
        if part.Parent.Name == 'Mop' then
            return true
        end
    end
end
 
spill.Touched:Connect(function()end)
 
while wait() do
    if checkIfTouchingMop() then
        if tween.PlaybackState ~= Enum.PlaybackState.Playing then
            tween:Play()
        end
    else
        if tween.PlaybackState ~= Enum.PlaybackState.Paused then
            tween:Pause()
        end
    end
end

Doesn’t seem to work, but the code looks correct

Magnitude would be very inaccurate unless the shape is a sphere.

1 Like

The part is a sphere, so it will work

Have you tried adding a wait() after TouchEnded?

script.Parent.TouchEnded:Connect(function(plt)
	wait(.1)
	if plt.Parent.Name == "Mop" then
		print("kk")
	--	tween:Pause()

       
	end
end)

If that really doesn’t work, as previously mentioned, you could use GetTouchingParts.


function searchForNameInTable(table,name)
	for i,v in pairs(table) do
		if v == name then
			return true
		end
	end
	return false
end

script.Parent.Touched:Connect(function(touch)
	if touch.Parent.Name == "Mop" then
		tween:Play()		
		repeat wait() until (searchForNameInTable(script.Parent:GetTouchingParts(),"Mop")==false)
	end
end)

there’s probably a better way to do it still but those are viable options

If the spills are set to CanCollide = false, then it won’t work unless you connect some kind of .Touched event to the spill.

I just edited the Pastebin I posted to add this, see if it works.

I’ll try it, I will respond after tested

magnitude works like that :
bruh
the red part is the spill,
the blue part is the mop,
and the purple one is the magnitude,
even if I don’t really recommend it isn’t very buggy and like what @EDmaster24 said
and you could make it check if they are touching

it works, thank you! ill accept ur answer

Just a resource to help understand how this works.