Could someone please help me with my script? I am uncertain as to why it is killing me despite the absence of a moving part heading in my direction. Any help would be greatly appreciated.
-- Get the position of the player character
local charPosition = game.Players.LocalPlayer.Character.HumanoidRootPart.Position
-- Create a box that encompasses 20 blocks in front of the character
local boxSize = Vector3.new(20, 20, 20)
local boxCFrame = CFrame.new(charPosition + Vector3.new(10, 0, 0), charPosition)
-- Use GetPartBoundsInBox to find any parts that are in the box
local parts = game.Workspace:GetPartBoundsInBox(boxCFrame, boxSize)
-- Check each part to see if it is moving and if it will hit the character's torso
for _, part in pairs(parts) do
local partVelocity = part.Velocity
local partPosition = part.Position
local partToCharVector = charPosition - partPosition
local partToCharDistance = partToCharVector.magnitude
-- If the part is moving and it is closer than 20 blocks from the character,
-- then it might hit the character's torso; destroy my Head
if partVelocity.magnitude > 0 and partToCharDistance < 20 then
if partVelocity:Dot(partToCharVector) > 0 then
-- If the part is moving towards the character,destroy my characters head
game.Players.LocalPlayer.Character.Head:Destroy()
end
end
end
