#Check collision of ONE player body part

1 messages · Page 1 of 1 (latest)

zealous frost
#

I'm following a introductory tutorial for roblox studio scripting and am trying to modify it by myself to learn more, so it's meant to be a platformer where each second spent jumping around increases your points.

I'm attempting to make a spawn zone, where if the player is in it (even if JUST their hand is in it) it doesn't increase the player's points.

The problem I'm encountering, since I'm using BasePart.Touched and BasePart.TouchEnded to check for whether the player is in or out of the zone (which is just a block part), is that, the player's left hand could be out of the zone, but their right one isn't, so then it bugs out and says that the player is both in and out of the zone.

I would ideally want to check if the player's head is intersecting another part.

safe echo
#
BasePart.Touched:Connect(function(part)
  if part.Name == "Head" then
    -- put rest of code here
    -- someones probably gonna criticize me for unoptimized code or something but it works
  end
end)
zealous frost
#

wdym "unoptimized"

safe echo
#

i dont know

#

usually whenever i do something simple someone calls me out 'oh you can just do this'

vocal tapir
#

that should work, but I would do an extra check to make sure a humanoid object exists aswell, just in case if there is any other part named "head" for whatever reason

safe echo
#

^

zealous frost
vocal tapir
#

either that or you can use workspace:GetPartsInPart, and connect this to a RunService.Heartbeat to check for whever a player is in the zone or not, but this one can be a little wonky to use and might be less efficient

vocal tapir
#

just another option, the first one works though 100%

queen nacelle
#

pretty classic issue , anyways for solution

track touching parts count ( most simple way )

local Players = game:GetService("Players")
local spawnZone = workspace.SpawnZone

local playersInZone = {}

spawnZone.Touched:Connect(function(hit)
    local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
    if humanoid then
        local player = Players:GetPlayerFromCharacter(hit.Parent)
        if player then
            playersInZone[player] = (playersInZone[player] or 0) + 1
            print(player.Name .. " entered zone (parts touching: " .. playersInZone[player] .. ")")
        end
    end
end)

spawnZone.TouchEnded:Connect(function(hit)
    local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
    if humanoid then
        local player = Players:GetPlayerFromCharacter(hit.Parent)
        if player then
            playersInZone[player] = (playersInZone[player] or 1) - 1
            if playersInZone[player] <= 0 then
                playersInZone[player] = nil
                print(player.Name .. " fully left zone")
            end
        end
    end
end)

-- checker
local function isPlayerInZone(player)
    return playersInZone[player] ~= nil
end
#

for the head-only detection using Region3 or raycasting

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local spawnZone = workspace.SpawnZone

local playersInZone = {}

local function isPointInZone(position)
    local zonePosition = spawnZone.Position
    local zoneSize = spawnZone.Size
    
    local minBounds = zonePosition - zoneSize/2
    local maxBounds = zonePosition + zoneSize/2
    
    return position.X >= minBounds.X and position.X <= maxBounds.X and
           position.Y >= minBounds.Y and position.Y <= maxBounds.Y and
           position.Z >= minBounds.Z and position.Z <= maxBounds.Z
end

RunService.Heartbeat:Connect(function()
    for _, player in pairs(Players:GetPlayers()) do
        if player.Character and player.Character:FindFirstChild("Head") then
            local head = player.Character.Head
            local wasInZone = playersInZone[player]
            local isInZone = isPointInZone(head.Position)
            
            if isInZone and not wasInZone then
                playersInZone[player] = true
                print(player.Name .. " head entered zone")
            elseif not isInZone and wasInZone then
                playersInZone[player] = nil
                print(player.Name .. " head left zone")
            end
        end
    end
end)

--checker again
local function isPlayerInZone(player)
    return playersInZone[player] == true
end
zealous frost
queen nacelle
#

welcome anytime enjoy

zealous frost
#

How do i close this post since it's solved

vocal tapir
#

no need to close it

zealous frost
#

alright

narrow gorge
#

if hit.parent:findfirstchild(“Humanoid”) or hit.parent.parent:findfirstchild(“Humanoid”) then