.Touched and .TouchEnded

Hey so I’ve got a script that detects when a vehicle hits a part that I use for detector. If the vehicle gets inside the part, it will make the value false. Then if someone wants to spawn a vehicle, it’ll search the first detector that doesn’t have a vehicle in (so true). It works all good, but. It keeps turning the value to true and false when the vehicle didn’t leave the part, when it’s just moving inside. Anyone know what to do?

The script:

script.Parent.Touched:Connect(function(Part)
	print(1)
	if Part:FindFirstAncestor("Vehicle") then
		print(2)
		local Vehicle = Part:FindFirstAncestor("Vehicle")
		print(3)
		
		if script.Parent.CanSpawn.Value == true and script.Parent.Object.Value == nil then
			print(4)
			script.Parent.CanSpawn.Value = false
			script.Parent.Object.Value = Vehicle
			print(5)
		end
	end
	wait(1)
end)

script.Parent.TouchEnded:Connect(function(Part)
	if Part:FindFirstAncestor("Vehicle") then
		local Vehicle = Part:FindFirstAncestor("Vehicle")

		if script.Parent.CanSpawn.Value == false and script.Parent.Object.Value == Vehicle then
			script.Parent.CanSpawn.Value = true
			script.Parent.Object.Value = nil
		end
	end
	wait(1)
end)

Try using this script and let me know if it’s better! Not sure, just testing

local reallyLeft = false

script.Parent.Touched:Connect(function(Part)
    
    local Vehicle = Part:FindFirstAncestor("Vehicle")
	if Vehicle  then
		if script.Parent.CanSpawn.Value == true and script.Parent.Object.Value == nil then
            reallyLeft = false
			script.Parent.CanSpawn.Value = false
			script.Parent.Object.Value = Vehicle
		end
	end
end)

script.Parent.TouchEnded:Connect(function(Part)
	local Vehicle = Part:FindFirstAncestor("Vehicle")
	if Vehicle  then
		if script.Parent.CanSpawn.Value == false and script.Parent.Object.Value == Vehicle then
		    reallyLeft = true
            spawn(function() 
                wait(.2) 
                if reallyLeft == true then 	
                    script.Parent.CanSpawn.Value = true
			        script.Parent.Object.Value = nil
                end
            end)
		end
	end
end)

Oh well I tried but it sadly continues to make the value true and false over and over

.Touched is very glitchy and has some questionable behavior. use FindPartsInRegion3WithWhiteList

I made a thread on this issue a while ago

Give it a read, I think it should solve your problem

Haha, I am kinda bad in scripting still. It’s confusing but I think I get most of it

yeah no it’s too complicated for my little brain, sorry.