How do I detect multiple players in one part?

I want to make it so when you punch two people at a time, they both get hit. However, for some reason only one random person gets hit.

Script:

script.Parent.OnServerEvent:Connect(function(Player)
	local Hitbox = Instance.new("Part", game:GetService("Workspace"))
	Hitbox.Size = Vector3.new(4,2,3)
	Hitbox.Material = Enum.Material.Neon
	Hitbox.BrickColor = BrickColor.new("Really red")
	Hitbox.Transparency = .75
	Hitbox.CanCollide = false
	Hitbox.Massless = true
	

	local Weld = Instance.new("Weld", Hitbox)
	Weld.Part0 = Hitbox
	Weld.Part1 = Player.Character.Torso
	
	Weld.C0 = Hitbox.CFrame:ToObjectSpace(Hitbox.CFrame * CFrame.new(0,0,2))

	local Damage = 8

	local OnCooldown = false

	Hitbox.Touched:Connect(function(Hit)
		if not Hit:IsDescendantOf(Player.Character) and Hit and Hit.Parent then
			local Humanoid = Hit.Parent:FindFirstChild("Humanoid")
			if Humanoid then
				if OnCooldown == false then
					OnCooldown = true
					
					Humanoid:TakeDamage(Damage)
					
					local p1 = game.ReplicatedStorage.Sounds.Punched1:Clone()
					local p2 = game.ReplicatedStorage.Sounds.Punched2:Clone()
					local p3 = game.ReplicatedStorage.Sounds.Punched3:Clone()
					local p4 = game.ReplicatedStorage.Sounds.Punched4:Clone()
					
					local hit1 = Humanoid:LoadAnimation(script.H1)
					local hit2 = Humanoid:LoadAnimation(script.H2)
					local hit3 = Humanoid:LoadAnimation(script.H3)
					local hit4 = Humanoid:LoadAnimation(script.H4)
					local ran = math.random(1,4)
					
					if ran == 1 then
						
						hit1:Play()
						p1.Parent = Humanoid.Parent.Torso
						p1:Play()
						
					elseif ran == 2 then
						
						hit2:Play()
						p2.Parent = Humanoid.Parent.Torso
						p2:Play()
						
					elseif ran == 3 then

						hit3:Play()
						p3.Parent = Humanoid.Parent.Torso
						p3:Play()
						
					elseif ran == 4 then

						hit4:Play()
						p4.Parent = Humanoid.Parent.Torso
						p4:Play()
						
					end
					
					if Humanoid.Parent.Name ~= ("Ball") then
					
					local Hplr = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)
					game.ReplicatedStorage.RemoteEvents.Stunned:FireClient(Hplr)
						
					end
				end
			end
		end
end)
	
	
	wait(.2)
	
	Hitbox:Destroy()
	
end)

I want them both to be getting hit, but it just hits a random one. How do I fix this?

2 Likes

You check that the variable OnCooldown is false, set it to true, then don’t set it to false afterwards.

1 Like

Maybe use :GetPartsInPart() instead of .Touched ? Never tried Raycast but you could try checking it out too
GetPartsInPart() would return a table with every Part currently inside the part given as an argument.

1 Like

so


if OnCooldown == false then
OnCooldown = true

then don’t set it back to false? but thats how it already is

1 Like

would you be able to show an example of what you mean? cause I just tried it and it’s giving me errors

1 Like

Which means the event can only activate once, instead of being able to hit multiple players. You need to set OnCooldown to false for a hit to register again.

1 Like

Yeah I assumed that, but the problem is if I do that then itll hit their arms and legs, whichll multiply the damage done.

1 Like

let’s assume we have a Part named “Hitbox” inside of a model in workspace.
You would write…
workspace:GetPartsInPart(workspace.Model.Hitbox)

It then returns a table with the Instances currently inside workspace.Hitbox

More info about this
https://create.roblox.com/docs/reference/engine/classes/WorldRoot#GetPartsInPart

1 Like

You could record the players who were hit in a table, and check if that player was hit before when the hitbox is touched.

-- OnCooldown was removed in this script, relying on hitPlayers to manage duplicate hits
local hitPlayers = {}

Hitbox.Touched:Connect(function(Hit)
		if not Hit:IsDescendantOf(Player.Character) and Hit and Hit.Parent then
			local Humanoid = Hit.Parent:FindFirstChild("Humanoid")
			if Humanoid then
-- code shortened here.............
				if Humanoid.Parent.Name ~= ("Ball") then
					local Hplr = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)
					if not table.find(hitPlayers, Hplr) then
						table.insert(hitPlayers, Hplr)
						game.ReplicatedStorage.RemoteEvents.Stunned:FireClient(Hplr)
					end

Sorry if my above post is days late, I posted it and it was put into an moderation queue to be approved. :face_with_diagonal_mouth: