I’m attempting to make a system that slowly damages a player when they’re inside of a part using GetPartsInPart() to detect if the player is inside of the part.
However, when the player is fully encapsulated in the part the script just stops working and only does anything if the player has contact with a face on said part.
I’ve looked through numerous scripting support posts and still just seem confused and stuck with this. This is my first time using GetPartsInPart() so it’s definitely possible that it’s an error on my end.
This script is placed inside of the kill part.
while wait(0.5) do
local Humanoids = {}
local parts = game.Workspace:GetPartsInPart(script.Parent)
for i = 1, #parts do
local h = parts[i].Parent:FindFirstChildWhichIsA("Humanoid")
if h ~= nil then
if table.find(Humanoids, h) == nil then
h.Health -= 5
table.insert(Humanoids, h)
end
end
end
end
I would personally make it a localscript (since exploiters can manipulate their character position anyway), and then just check if their character is below the water.
local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Root = Character.PrimaryPart or Character:WaitForChild("HumanoidRootPart")
local Water = workspace:WaitForChild("WaterPart")
while task.wait(.5) do
if not Humanoid or Humanoid.Health == 0 then break
if ( Root.Position.Y + Humanoid.HipHeight ) > ( Water.Position.Y + (Water.Size.Y/2) ) then continue end
Humanoid:TakeDamage(5)
end
if Water is not always present, then just define & check for Water inside the while loop.
Root position and hipheight combined, is just a way to tell the script that we want them to be a bit below water before taking damage. You could also just get (head.position + (head.size.y/2)) and define that as “under water, take damage now”.
Water position y + water size y divided by 2, just tells the script to get the position of the water and get the very top face, and define that as the “water/sealine”, where they start to take damage.