Touched event doesn't fire consistently

Hello,
I was working with touchable parts until I was stuck struggling on how to fix this inconsistent touch function.

Video:

It would be greatly appreciated if you could help me with this.

could you send the code? We can’t really tell what’s happening or going wrong if we can’t see it.

The touched event fires every time a part moves a bit inside the part, You can use a debounce to avoid this issue

local Debounce = false

Part.Touched:Connect(function()
	if Debounce then
		return
	end

	Debounce = true

	task.wait(1)

	Debounce = false
end

local part = script.Parent

local function PlayerTouched(Part)

local Parent = Part.Parent

if game.Players:GetPlayerFromCharacter(Parent) then

part.BrickColor = BrickColor.new("Really red")
 
end

end
part.Touched:connect(PlayerTouched)

part.TouchEnded:Connect(function()

part.BrickColor = BrickColor.new("Lime green")

end)

Hi,

Have you tried to make an invisible non-cancollide part, that is larger than the button in the Y-axis, and let that work as the detector? That is the only method I use, every time I use a touched event, since the touch event can be a bit buggy otherwise.

As @TortenSkjold said, make a larger invisible hit box above the button, however, I would also add to check only for the HumanoidRootPart, check for touched and touchended

1 Like

Stable Button Hit.rbxl (41.5 KB)

image

image

local hitBox = script.Parent
local button = hitBox.Parent:WaitForChild("Button")

hitBox.Touched:Connect(function(part)
	if part.Name == "HumanoidRootPart" then
		local player = game.Players:GetPlayerFromCharacter(part.Parent)
		if player and hitBox:GetAttribute("HitPlayer") == nil then
			hitBox:SetAttribute("HitPlayer",player.UserId)
			button.BrickColor = BrickColor.new("Really red")
		end 
	end
end)

hitBox.TouchEnded:Connect(function(part)
	if part.Name == "HumanoidRootPart" then
		local player = game.Players:GetPlayerFromCharacter(part.Parent)
		if player and hitBox:GetAttribute("HitPlayer") == player.UserId then
			hitBox:SetAttribute("HitPlayer",nil)
			button.BrickColor = BrickColor.new("Lime green")
		end 
	end
end)
2 Likes

Please format it lmao… My eyes hurt

Here is the sample that SelDraken talked about:

local Debounce = nil
local Detector = ..-- Define yourself

Detector.Touched:Connect(function(Hit)
	local Char = Hit.Parent
	local Hum = Char:FindFirstChildOfClass("Humanoid")
	if Hum and Hum.Health > 0 and Debounce ~= Char.Name then
		Debounce = Char.Name
		--ChangePartColorTrue
	end
end)
Detector.TouchedEnded:Connect(function(Hit)
	local Char = Hit.Parent
	local Hum = Char:FindFirstChildOfClass("Humanoid")
	if Hum and Hum.Health > 0 and Debounce == Char.Name then
		Debounce = nil
		--ChangePartColorFalse
	end
end)

Edit: Seems like SelDraken got a similar one.

Thank you for the help,
I think it really helped that it was only trying to identify 1 core part

1 Like

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