Checking which players are touching a part

So basically I’m making a gas leak in my game and i want to have a safety room. My way of achieving this is by making a part that fills out the entire floor. My problem comes when i try to check which players are touching this when a timer is done. I honestly have no idea how to do this! I tried to use a touched and touch ended but i couldn’t make it work with multiple players. Any ideas would be much appreciated! :slight_smile:

I reckon getpartsinparts() would work better for what you’re trying to do, take a look at this:

i see but how would i check specifically which players are touching and do something to those who aren’t

if you used getpartsinpart() then youd want to find the player using the parts it gives, by doing something like part.parent which will give you the character, i dont recommend doing it like that because there are loads of issues with it but itll work for the time being. Once the timer ends add all the players found in the part to a table and then you have a table of all players inside the part. I dont have the time to explain it completely unfortunately but hopefully that gives you an idea

local Players = game:GetService("Players")
	
	local function getCharacter(part: BasePart)
		local model = part:FindFirstAncestorOfClass("Model") 
		if model:IsA("Tool") then -- Tool nows return model
			return model.Parent,  Players:GetPlayerFromCharacter(model.Parent)
		end
		return model, Players:GetPlayerFromCharacter(model)
	end
	
	for _,v in Part:GetTouchingParts() do -- :GetPartsInPart() if all goes wrong.
		local Character: Model?, Player:Player? = getCharacter(v)
		if not Player then
			print(Character:GetFullName(), "is either an NPC or a non player.")
			continue
		end
		print(Character:GetFullName(), Player:GetFullName())
	end
1 Like

Hey nice code, you should also make it so it only checks only one player at a time. Since if more than one limbs touch the part it might cause confusion when you issue the same code on the same Player twice. :smiley:

Also you don’t have to make another :IsA(“Tool”) check since FindFirstAncestorOfClass only checks an Instance’s ClassName!

1 Like

Got it confused for FindFirstAncestorWhichIsA, oops.
image

1 Like

If i’m being completely honest, you could use a module called ZonePlus basically all this module does is simplify Region3, what Region3 does is it allows you to check players in an Area, basically by using 2 different position’s, but with ZonePlus, you could instead turn a Part into a Region3, which makes it easier to detect who is inside the Region.

You could also use something called :GetPartBoundsInRadius which is basically just searching a circular/spherical area instead of a cubic one.

I’m going to create a script without it though, first of i’ll be using :GetTouchingParts, and if you’d want tick damage, you’d make a debounce table. (a table with the names of players who are currently on cooldown from taking damage)

local RunService = game:GetService("RunService") -- getting the runservice which allows loops

local part = script.Parent

local debounce = {}
local cooldownTime = 1 -- the time that elapses before the play takes tick damage again

function checkPlayers()
	local d 
	
	d = part.Touched:Connect(function() -- prevents the function from running multiple times
		d:Disconnect()
	end)
	
	for _, v in pairs(part:GetTouchingParts()) do -- loops through touching
		local h = v.Parent:FindFirstChildOfClass("Humanoid")
		
		if h then -- if humanoid found then
			if debounce[h.Parent.Name] then
				continue
			else			
				debounce[h.Parent.Name] = { -- player inserted into the table where they take tick damage
					timeElapsed = tick()
				}
					
				h:TakeDamage(7)
			end
		end
	end
end

RunService.Heartbeat:Connect(function()
	checkPlayers() -- run the function
	
	for i, v in pairs(debounce) do -- loop through the table
		if tick() - v.timeElapsed >= cooldownTime then
			debounce[i] = nil -- if the elapsed time is over the cooldown time, then it will remove the player from the debounce table, making them subject to taking damage
		end
	end
end)

I made it work by using GetPartsInPart! Here’s my script incase anyone are interested:

local parts = workspace:GetPartsInPart(workspace.Michellsanus.ElectricityRoomSafetyPart)
print("Touching parts: ", parts)
for i, v in pairs(parts) do
if v.Parent:FindFirstChild(“HumanoidRootPart”) then
print("Player: ", v.Parent.Name)
local foundPlayer = table.find(playerNames, v.Parent.Name)
if (foundPlayer) then
print("Found player: ", foundPlayer)
table.remove(playerNames, foundPlayer)
end
end
end

(I still haven’t made the part where i kill the player yet.) Thank you all for your help😊

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