How to fix this touch ended script?

Hi,
So i have a script that should check who touched part last time and if this was “Villager” (its npc name) then it will make a bool values value true, but for some reason this script don’t count npc. For example i will check if thing that last time touched part was named “HumanoidRootPart” and if npc goes away then value do not changing but if i will touch part and go away script will change. I don’t know why but this script don’t see npc. npc is not local so i don’t know.

script.Parent.TouchEnded:Connect(function(hit)
	if hit.Name == "Villager" then
		script.Parent.BoolValue.Value = true
	end
end)

use .Parent always

script.Parent.TouchEnded:Connect(function(hit)
	if hit.Parent.Name == "Villager" then
		script.Parent.BoolValue.Value = true
	end
end)

i tryed this and also tryed to check name of parent of npc still nothing.

the script’s Parent is an object right?

yes

Screenshot_180

are you checking for the player or the npc

i want to check if npc went away from this part so npc
check if npc no more touching it

but did you use a touched event before this

what do you mean? (30 characters needed)

your code will work if you used a Touched Event then you can just do a touch ended if you use A touch ended event

a sample code

local part = script.Parent
 
local function onPartTouchEnded(otherPart)
	print(part.Name .. " is no longer touching " .. otherPart.Name)
end
 
part.TouchEnded:Connect(onPartTouchEnded)

try converting your code to this

local parent = script.Parent

local function Functions(otherPart) -- why do we check if there is a humanoid because it might not be a fake player it might be a object
       if otherPart:FindFirstChild("Humanoid") and otherPart.Name == "Villager" then
             script.Parent.BoolValue.Value = true
       end
end

parent.TouchEnded:Connect(Functions)

PS: Your code will only work if the bool value is false