“Touched” is endlessly turned on and off

  1. What do you want to achieve? Keep it simple and clear!
  2. What is the issue? Include screenshots / videos if possible!
    when the player moves, “Touched” is endlessly turned on and off. If it does not move then it is turned on as it should be.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

LocalScript:

part.Touched:Connect(function()
	sound.ReverbSoundEffect.Enabled = true
end)

part.TouchEnded:Connect(function(pt)
	if part.Touched ~= pt.Parent:FindFirstChild("Humanoid") then
		sound.ReverbSoundEffect.Enabled = false
	end
end)

See if this helps any…

part.Touched:Connect(function(source)
  local player = game.Players:GetPlayerFromCharacter(source.Parent)
  if player and not player:GetAttribute("IsInBlock") then
    player:SetAttribute("IsInBlock",true)
    sound.ReverbSoundEffect.Enabled = true
  end
end)

part.TouchedEnded:Connect(function(source)
  local player = game.Players:GetPlayerFromCharacter(source.Parent)
  if player then
    player:SetAttribute("IsInBlock",false)
    sound.ReverbSoundEffect.Enabled = false
  end
end)

Something I learned later after posting this, is that you also need to check if the part that triggered the Touched or TouchEnded, ‘source’ in the code above, check if it is a HumanoidRootPart, and make sure the trigger block ‘part’ is high enough to hit the HumanoidRootPart. Only do the steps in the code if its a HumanoidRootPart, this keeps smaller parts, such as hands, and feet etc… from triggering the events, if they move out of the area of the part being touched.

part.Touched:Connect(function(source)
  local player = game.Players:GetPlayerFromCharacter(source.Parent)
  if player and source.Name == "HumanoidRootPart" and not player:GetAttribute("IsInBlock") then
    player:SetAttribute("IsInBlock",true)
    sound.ReverbSoundEffect.Enabled = true
  end
end)

part.TouchedEnded:Connect(function(source)
  local player = game.Players:GetPlayerFromCharacter(source.Parent)
  if player and source.Name == "HumanoidRootPart" then
    player:SetAttribute("IsInBlock",false)
    sound.ReverbSoundEffect.Enabled = false
  end
end)

Something like this.

1 Like