How to detect if a part stopped touching another part

hi there,
im making a sort system where it checks if a food is on the ground and is setting a boolval in rep storage parented in a folder named bools to true when the script detects a food obj on the ground. That part was doable but when i wanted to script that when the food was not touching the part anymore is where i needed help. I noticed that when the food isnt touching the floor then the script stops running that lines of code. Here is my script:

while task.wait() do
	local part = script.Parent
	
	local touching = part:GetTouchingParts()
	local isTouching = false

	for i, touchingPart in pairs(touching) do
		if touchingPart:IsA("BasePart") or touchingPart:IsA("MeshPart") then
			if touchingPart:GetAttribute("Food") then
				isTouching = true
				local pPartTouching = touchingPart:GetTouchingParts()
				game.ReplicatedStorage:WaitForChild("Bools"):WaitForChild("FoodOnGround").Value = true
				print("Food on ground by: " .. touchingPart.Name)
				print(game.ReplicatedStorage:WaitForChild("Bools"):WaitForChild("FoodOnGround").Value)
				for i, pa in pPartTouching do
					if not pa:GetAttribute("Ground") then
						isTouching = false
						game.ReplicatedStorage:WaitForChild("Bools"):WaitForChild("FoodOnGround").Value = false
						print(game.ReplicatedStorage:WaitForChild("Bools"):WaitForChild("FoodOnGround").Value)
					end
				end
			end
		end
	end
end

if you have a solution to how i can still make it check if the food stopped hitting the floor and setting the bools value to false, then i would love to hear that.

Is there a good reason for using BoolValues in ReplicatedStorage, instead of for example just setting an attribute to the food parts? Also is there a good reason to be tracking this at all? If it’s used for something along the lines of: an event takes place (e.g. a ProximityPrompt is activated), and then you check whether the BoolValue used for that food object is true or not - then you don’t need to be doing this at all, just do the GetTouchingParts check when that event happens.

If there is a valid reason for keeping track, then you can use something like this:

local OnTheGround -- create or find the BoolValue

local function onTouched(OtherPart: BasePart)
    if OnTheGround.Value then return end
    if OtherPart:GetAttribute('Ground') then OnTheGround.Value = true end
end

local function onTouchEnded()
    if not OnTheGround.Value then return end
    for _, Part in Food:GetTouchingParts() do
        if Part:GetAttribute('Ground') then return end
    end
    OnTheGround.Value = false
end

Food.Touched:Connect(onTouched)
Food.TouchEnded:Connect(onTouchEnded)
for _, Part in Food:GetTouchingParts() do onTouched(Part) end

Obviously set it up however it suits you and do it for every food part. This is a simple, slightly inefficient method, but still much more efficient than running a loop.

But again, I advise against the whole tracking system if you don’t need to for example listen to changes (when a food starts or stops touching the ground). And if you do need it I advise switching to attributes instead of BoolValues, they also replicate and will make your code much easier to read and maintain.

Well i was planning to do it like that so when other things need to know if the food is touching the floor it then can know, my knowledge isnt that big so i did what i know.

In that case (sounds like you don’t need events) you can just use a function like this…

local function isTouchingGround(Food: BasePart)
    for _, Part in Food:GetTouchingParts() do
        if Part:GetAttribute('Ground') then return true end
    end
end

… in whichever Script or LocalScript that needs to know. Use the function on an event such as ProximityPrompt.Triggered, or whatever suits your needs.

so for example a touched event? Because i need to knw what touched the ground right?

Its still not what i asked for, the question was on how to detect if a part stopped touching another part
found out that a event called touch ended exists :man_facepalming:

I gave you what you asked for in the first reply, which you apparently didn’t read, because now you “found out” about TouchEnded, which is used in the first reply.

My excuse for not reading, i gave you the solution now. Thanks for the help

1 Like

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