How can I check if any bodyparts of the player can be seen?

Hi, So I am using raycasting to see if a part can see the character right? And I want to make it so if it sees the character it losses health, I have tried putting the character as the target and that doesn’t work, How could I fix this?

Here’s the script:

local A = game.Workspace:WaitForChild("Dedector")

local C


game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(Player)
	C = Player.Character
	print(C.Name)
end)

wait(3)
while true do
local Direcetion = C:WaitForChild("HumanoidRootPart").Position - A.Position

local raycastResult = workspace:Raycast(A.Position, Direcetion)

	if raycastResult and raycastResult.Instance then
		if raycastResult.Instance == C then
			C.Parent:WaitForChild("Humanoid").Health -= 10
			end
end

	print(raycastResult)
	
	task.wait(1)
	end
1 Like

I didn’t quite understand your question, but in the result, you should go to the result.Instance.Parent and search for humanoid, then use Humanoid:TakeDamage() instead of subtracting directly the health, also, if you raycasting for the character, make sure you use raycast params because the ray may intersect a part of the character

Well I ment, I would like to check if the part can see any parts of the character right, And if it sees one bodypart of the character, That character would take damage

By seeing you mean about transparency, or casting a ray to see if the origin have a free way to get to the desired part?

To cast a ray to see if the origin have a free way to get to the desired part

Ok, I understand, so first I see is that you wait the character to be passed, but after 3 seconds the script will execute, and will run with or without the character, so it might error, also, checking raycast.instance will return a character limb, but not the character, so it will be always false

Ohhhh, But then how would I make it so if it is the bodyparts it still does the damage

So, result.Instance returns a limb, so you would use, result.Instance.Parent:FindFirstChild(“Humanoid”):TakeDamage()

1 Like

thank you! this actually works thank you!

1 Like

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