Why is `.Touched` glitching out?

I am trying to make a bush movement system. I am trying to make the bush randomly tweak and turn when a player walks in and out of the bush but for some reason, it keeps running the .Touched function and completely ignores the debounce that I have implemented. Here is what I mean and this is my code currently!

`
local TweenService = game:GetService(“TweenService”)

local Sound = script:FindFirstChild(“Foliage Movement 1 (SFX)”)
local Debounce = true

script.Parent.Touched:Connect(function(hit)
if game.Players:FindFirstChild(hit.Parent.Name) then
local Player = game.Players:FindFirstChild(hit.Parent.Name)

	if Player then
		if Debounce == true then
			Debounce = false
			local RandomVector = Vector3.new(math.random() * 10,math.random() * 10,math.random() * 3)
			local tween = TweenService:Create(script.Parent,TweenInfo.new(.2,Enum.EasingStyle.Quad,Enum.EasingDirection.InOut,0,false,0),{Orientation = RandomVector})
			local tween2 = TweenService:Create(script.Parent,TweenInfo.new(.2,Enum.EasingStyle.Quad,Enum.EasingDirection.InOut,0,false,0),{Orientation = Vector3.new(1.516, 31.97, -2.428)})

			tween:Play()
			Sound:Play()
			tween.Completed:Wait()
			tween2:Play()
			tween2.Completed:Wait()
		end
	end
end

end)

script.Parent.TouchEnded:Connect(function(hit)
if game.Players:FindFirstChild(hit.Parent.Name) then
local Player = game.Players:FindFirstChild(hit.Parent.Name)

	if Player then
		if Debounce == false then
			Debounce = true
			local RandomVector = Vector3.new(math.random() * 10,math.random() * 10,math.random() * 3)
			local tween = TweenService:Create(script.Parent,TweenInfo.new(.2,Enum.EasingStyle.Quad,Enum.EasingDirection.InOut,0,false,0),{Orientation = RandomVector})
			local tween2 = TweenService:Create(script.Parent,TweenInfo.new(.2,Enum.EasingStyle.Quad,Enum.EasingDirection.InOut,0,false,0),{Orientation = Vector3.new(1.516, 31.97, -2.428)})

			tween:Play()
			Sound:Play()
			tween.Completed:Wait()
			tween2:Play()
			tween2.Completed:Wait()
		end
	end
end

end)
`

P.S. Idk why it isnt all going into these brackets

Sorry, will use this message as a placeholder to fix the code

local TweenService = game:GetService(“TweenService”)

local Sound = script:FindFirstChild(“Foliage Movement 1 (SFX)”)
local Debounce = true

script.Parent.Touched:Connect(function(hit)
if game.Players:FindFirstChild(hit.Parent.Name) then
local Player = game.Players:FindFirstChild(hit.Parent.Name)

	if Player then
		if Debounce == true then
			Debounce = false
			local RandomVector = Vector3.new(math.random() * 10,math.random() * 10,math.random() * 3)
			local tween = TweenService:Create(script.Parent,TweenInfo.new(.2,Enum.EasingStyle.Quad,Enum.EasingDirection.InOut,0,false,0),{Orientation = RandomVector})
			local tween2 = TweenService:Create(script.Parent,TweenInfo.new(.2,Enum.EasingStyle.Quad,Enum.EasingDirection.InOut,0,false,0),{Orientation = Vector3.new(1.516, 31.97, -2.428)})

			tween:Play()
			Sound:Play()
			tween.Completed:Wait()
			tween2:Play()
			tween2.Completed:Wait()
		end
	end
end
end)

script.Parent.TouchEnded:Connect(function(hit)
if game.Players:FindFirstChild(hit.Parent.Name) then
local Player = game.Players:FindFirstChild(hit.Parent.Name)

	if Player then
		if Debounce == false then
			Debounce = true
			local RandomVector = Vector3.new(math.random() * 10,math.random() * 10,math.random() * 3)
			local tween = TweenService:Create(script.Parent,TweenInfo.new(.2,Enum.EasingStyle.Quad,Enum.EasingDirection.InOut,0,false,0),{Orientation = RandomVector})
			local tween2 = TweenService:Create(script.Parent,TweenInfo.new(.2,Enum.EasingStyle.Quad,Enum.EasingDirection.InOut,0,false,0),{Orientation = Vector3.new(1.516, 31.97, -2.428)})

			tween:Play()
			Sound:Play()
			tween.Completed:Wait()
			tween2:Play()
			tween2.Completed:Wait()
		end
	end
end
end)
2 Likes

Sorry its not all in one! I’m also trying to find a platform I can upload this video to so I can show you the problem!

My guess is that it’s a problem with the TouchEnded function, for example the debounce is immediately being set to true. Maybe you could do testing and print something like “touch ended” inside the function and see when it prints. Let me know what you find so I can help come up with a solution after you test.

Touched and TouchEnded are terribly touchy … unfortunately. Let say you have a plate and you walked on to it, you touched it. If you stop moving you’re not touching it. If you starting moving again while still on the plate you touched it again.

Only way I found around this was to have two touch items and use them together as just touched, the latter indicating the former is not being touched as they are far enough apart.

you can make one or both transparent

You could probably use BasePart:GetTouchingParts() to find any parts that are currently inside the bush. here is a link to learn more:
https://create.roblox.com/docs/reference/engine/classes/BasePart/GetTouchingParts

Also mentioned here, if you want to detect parts touching a part that has CanCollide off: GetTouchingParts() without CanCollide

Did you read (literally) the first line of that post before linking it?

It’s obsolete, please stop linking to that thread and link to Introducing OverlapParams - New Spatial Query API instead.

1 Like

What does that mean? It still works for me when I test it why is that?

It’s obsolete in that there is a replacement that is better. Namely, the new OverlapParams API.

Obsolete doesn’t always imply breakage, but it does imply deprecation.

1 Like

SORRY EVERYONE FOR THE LATE REPLIES! I have been troubleshooting all of your ideas and I just found out a solution to it. Instead of using the object that I am rotating as the hitbox for the .Touched, I am now using a stable hitbox. This way, it won’t move the hitbox out of the player and then bring it back in, then move it out, etc. Thank you for all your replies and suggestions!

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