If statement arent true for some reason

I want to make a magnitude combat script. I was making if statements to check how close the tool parent humanoid rootpart compared to the npc humanoidrootpart. And for some reason, it isn’t working. I was thinking of doing render stepped to constantly check how close it is, then see if it is below 5, then do damage. But for now, I have no idea where I am going wrong. Here is my script:

local players = game:GetService(“Players”)
local RS = game.ReplicatedStorage
local PunchEvent = RS:WaitForChild(“PunchEvent”)
local RepelEvent = RS:WaitForChild(“RepelEvent”)

local tool = script.Parent
equipped = false

tool.Equipped:connect(function()
equipped = true
end)

local damage = 5

local function check()
if equipped then
local character = tool.Parent
local humanoidRootPart = character.HumanoidRootPart
for i,player in pairs(game.Workspace.NPC:GetChildren()) do
if player.HumanoidRootPart.Position.Magnitude - character.HumanoidRootPart.Position.Magnitude >= 5 then
** print(“True”)**
** player.Humanoid:TakeDamage(5)**
else
print(“False”)
end
end
end
end

local function repelTest()

end

PunchEvent.OnServerEvent:Connect(check)
RepelEvent.OnServerEvent:Connect(repelTest)

Check the one bolded in black.

if (player.HumanoidRootPart.Position - character.HumanoidRootPart.Position).Magnitude < 5 then 
    print("True")
    player.Humanoid:TakeDamage(5)
else
    print("False")
end
1 Like

just use the dot product to compare the angle between both vectors

a · b = | a | × | b | × cos(θ)

What is the dot product? I have heard of it but I don’t know what it is

You do not need the dot product in this scenario, it’s just a simple magnitude check.

Thanks! I didn’t know you needed brackets for that

It works, but even if I go behind the npc, it still damages.

The dot product is really useful in your case because you can use it in your combat system!

Heres how you can use it.

Basically the dot product is the angle between two given vectors

A vector is something that has magnitude an direction basically

image

image

With that knowledge, you can detect if a player in front of the player or to the right or a bit to the left; and its very powerful and can be used a variety of ways

What you can do is get the object space CFrame of your character relative to the target.

local ObjectSpaceCFrame = NPC.HumanoidRootPart.CFrame:ToObjectSpace(YourCharacter.HumanoidRootPart.CFrame)

Then, just check if the position is negative in the Z axis like this:

if ObjectSpaceCFrame.Position.Z < 0 then print("IsBehind") end

THANK YOU FOR THE EXPLINATION!!! I really needed this!

Now since we found the angle between both vectors your player will be able to see whats in front of them rather then all around them

Thank you I got it. The dot product seems to be very very useful.

Yes it seriously is in some situations, another example would be if you want to make a stealth system and different npcs can detect things within the range of the angle that the dot product produces