Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

I want the enemy to take damage, only if im facing them (combat script)?

I didn't post the whole script, only the part im having trouble with.

Character is me (the player that is punching, passing with remote event, local -> server)
m is the other player.

I tried doing if Character is looking at the m they take damage, but now the enemy won't even take damage, even if i am facing or not, what am i Doing wrong?

	for i,m in pairs(game.Workspace:GetChildren()) do
		if m and m:FindFirstChild("Humanoid") and m ~= Character then
			if (Character.HumanoidRootPart.CFrame.LookVector - m.HumanoidRootPart.Position).Magnitude <= 8 then
				print("humanoid nearby")
				m.Humanoid.Health -= 10

1 comment

0

looking at m would require you to be directly looking at m. You might want to make the look at a little more broad for it to work.

1 answer

Haven't tested this out fully, but try this:

local wc = workspace:GetChildren()
local wcn = #wc
for i = 1, wcn do
	if wc[i] and wc[i]:FindFirstChild("Humanoid") and wc[i] ~= Character then
		local difvec = (Character.HumanoidRootPart.Position - wc[i].HumanoidRootPart.Position).Unit
		local dot = difvec:Dot(wc[i].HumanoidRootPart.CFrame.LookVector.Unit)
		
		if dot > 0 then
			print("Humanoid nearby")
			wc[i].Humanoid.Health = wc[i].Humanoid.Health - 10
		end
	end
end

The idea of this is to take the dot product of the vector between the player and the other character, and the player's look direction. If I did this correctly, this should result in the enemy being hit if they are within 180 degree field of view of your character's view direction.

This discussion is preserved as it was. New answers, comments, and votes are closed.