Touched event not detecting multiple players

So when the player presses Z it’s supposed to highlight any players that touch the hitbox for 3 seconds then make the highlight invisible. For some reason it only highlights one player and none of the others, and sometimes it does not highlight them at all.

--//Services\\--
local repStorage = game:GetService("ReplicatedStorage")
local repFirst = game:GetService("ReplicatedFirst")
local UserInputService = game:GetService("UserInputService")
local Lighting = game:GetService("Lighting")
local player = game:GetService("Players").LocalPlayer
local TweenService = game:GetService("TweenService")

local tweenInfo = TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut,0,true,0)
local tweenGoal = {Size=Vector3.new(120,60,120)}

local Assets = repFirst.Assets

local SixthSenseSound = Assets.Sounds.SixthSenseActivate
local SixthSenseHitbox = Assets.SixthSenseHitbox

local SixthSenseLighting = Lighting.SixthSense
local SixthSenseBlur = Lighting.SixthSenseBlur

local highlight = Instance.new("Highlight")

local Activated = false
local debounce = false

local keyPress = Enum.KeyCode.Z -- What button should we press to toggle.
while true do
	game.Players.LocalPlayer:GetMouse().KeyDown:Connect(function(key) -- Detects Button Press
		local hitboxClone = SixthSenseHitbox:Clone()
		if key == "z" and Activated == false then
			
			Activated = true
			SixthSenseLighting.Enabled = true
			SixthSenseBlur.Enabled = true
			SixthSenseSound:Play()

			hitboxClone.Parent = player.Character
			hitboxClone.CFrame = player.Character.HumanoidRootPart.CFrame

			local hitboxTween = TweenService:Create(hitboxClone,tweenInfo,tweenGoal)

			hitboxTween:Play()
			
			hitboxClone.Touched:Connect(function(hit)
				local target = hit.Parent
				
				if target and target:FindFirstChild("Humanoid") and target.Name ~= player.Character.Name then
					
					
					if not debounce then
						debounce = true

						print("Hitbox was touched")

						highlight.Parent = target
						highlight.FillColor = Color3.fromRGB(94, 222, 96)
						highlight.FillTransparency = 0.3

						wait(3)

						highlight.FillTransparency = 1
						debounce = false

					end
					
				end
				
			end)
			
			wait(1)
						
			hitboxClone:Destroy()
			
			wait()
			
			Activated = false
			SixthSenseLighting.Enabled = false
			SixthSenseBlur.Enabled = false

		end
	end)
	wait(5)
end

First off I’m guessing that this is a Server Script, otherwise it’d only work for the local player.

I’m don’t know enough about why it isn’t working but here are my thoughts:

I think you need to loop through every player that’s touching the hitbox.
As it is now when the local player presses Z it checks to see if someone is touching the hitbox with hitboxClone.Touched:Connect(function(hit). That only fires once when another player touches the hitbox, so if other players are touching it during that first “Z” loop it won’t highlight them as well.

Also:

  • Why do you clone the Hitbox every time the player presses a button? Shouldn’t it only be when the press Z?
  • Why are you using wait instead of task.wait?

to add onto this, you are also running a connection inside of a while true loop without ever disconnecting it (from what I can see) which is going to cause memory leaks.

What changes would you suggest? I am rather new to this, and I don’t know what task.wait is. This is inside a local script.

A local script only works for the local player (afaik, I could be completely wrong on this).
Also, check create.roblox.com if you ever wonder about anything Roblox, like task.wait. wait() has been deprecated and replaced by task.wait().

I’ll do some edits and see if anything changes.

also, I would recommend looking into how connecting and disconnecting connections work. You are creating unnecessary connections that will cause lag over time with

game.Players.LocalPlayer:GetMouse().KeyDown:Connect(function(key)

since you are doing this in a while true loop

How do I make it loop through the characters that are touching it?

I was able to find it out, thanks guys.

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