Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I get the number of players touching a part? (Touched event)

I trying to make a part if there are 3 or more people walk on that. they'll die, but i dont know why is it not working.

	local Part = script.Parent
	Part.Touched:Connect(function(hit)
for i,v in pairs(#hit.Parent.Name:GetChildren()) do --- Need fix
							if i > 3 then
								print("Zomb")
								Zomb()
								else break
							end
end)

2 answers

✓ Accepted answer

You could try this but I don't know if it works because I didn't try it

local part = script.Parent -- The Part

local Touching = {	} -- Everyone who touches the part

part.Touched:Connect(function(hit) -- Fires when someone touches it
	local char = hit.Parent -- Gets the character
	if game.Players:GetPlayerFromCharacter(char) then -- Checks to see if it is actually a player
		table.insert(Touching, game.Players:GetPlayerFromCharacter(char)) -- Adds them to the touching table
	end
	if #Touching >= 3 then -- if there is 3 or more people touching
		for i,v in pairs(Touching) do
			v.Character.Humanoid.Health = 0 -- kills
			table.remove(Touching, i) -- removes player from the table
		end
		
	end
end)

part.TouchEnded:Connect(function(hit)
	local char = hit.Parent -- Gets the character
	if game.Players:GetPlayerFromCharacter(char) then -- Checks to see if it is actually a player
		for i,v in pairs(Touching) do
			if v.Name == game.Players:GetPlayerFromCharacter(char).Name then -- finds where player is located in the table
				table.remove(Touching, i) -- removes player from table
			end
		end
	end
end)

1 comment

0

your idea works. I just need to adjust something. Thank you!

You can try making a number value and then in your script put:

local part = script.Parent

part.Touched:Connect(function(hit)
	local PlrsTouchingCount = script.Parent.NumberValue --I said NumberValue but make sure to change this into the name of your number value
	PlrsTouchingCount.Value = PlrsTouchingCount.Value+1
end)

--Now we are gonna make it so when they are not touching the part then it doesnt count them in the NumberValue

part.TouchEnded:Connect(function(hit)
	local PlrsTouchingCount = script.Parent.NumberValue --Remember change the word NumberValue into the name of your real NumberValue
	PlrsTouchingCount.Value = PlrsTouchingCount.Value-1
end)

Hope this helps :D

3 comments

0

wait i just noticed you wanted it at 3! Oop lol

0

Sorry about that i'm fixing the script rn

0

I want it to be like if there is a 3rd person touch the part, then 3 of them will die

This discussion is preserved as it was. New answers, comments, and votes are closed.