HI there!
I’m Glade and I’m having a problem with my repeat loop. I have an if statement in my repeat loop but it doesn’t work.
repeat
spPath:Run(target.HumanoidRootPart.Position)
NPC:SetAttribute("Chasing", true)
curTarget = target
if Magnitude < 3 then
jumpscare(target)
end
until target.Humanoid.Health < 1 or Magnitude > leaveDistance or NPC.Humanoid.Health < 1
The first step to debugging (ideally before you ask for help) is to check what the variables are. The if statement isn’t working. if statements always work, but what’s inside might not.
First, check if we’re making it past the if check. print(Magnitude) before that line and print(“Passed!”) after that line. If it makes it through, figure out why the function isn’t running properly.
If it didn’t, what did Magnitude print? More than 3? You’ll need to figure out why. There’s a possibility that Magnitude isn’t being calculated correctly, and there’s a chance that it just isn’t being updated. The most likely of these two options, given what I can see, is that it isn’t being updated.
If you did this earlier in the script: Magnitude = (a-b).Magnitude, then it copied this number into the Magnitude variable and then it won’t update the variable any more. Programming works differently from math. In math, two things that are equal will always be equal. In programming, the = operator only assigns a value to a variable. I apologize if you already know this.
Here you can see that even though we set Magnitude to equal dist, it won’t keep up with dist. If this is your problem, you need to move the Magnitude calculation into your loop so that it gets updated.
If that’s not your problem, then let’s take a look at how you’re calculating Magnitude.
If the if statement is succeeding but still nothing happens, we’ll need to take a look at jumpscare function.
Well if you don’t understand your IF statement you need to know what magnitude is:
Magnitude in roblox uses vector theory.
That essentially means:
Square Root of (x^2 + y^2 + z^2)
Magnitude works off of both Position and Size
function canSee(tar)
local ori = RootPart.Position
local dir = (RootPart.Position - tar.HumanoidRootPart.Position).Unit * maxDistance
local ray = Ray.new(ori, dir)
local hit, pos = workspace:FindPartOnRayWithIgnoreList(ray, {script.Parent})
local status = false
if hit then
if hit:IsDescendantOf(tar) then
local unit = (RootPart.Position - tar.HumanoidRootPart.Position).Unit
local lv = RootPart.CFrame.LookVector
local dot = unit:Dot(lv)
if dot > 0 then
status = true
end
else
status = false
end
end
return status
end
– AI CHECK
function chaseTarget(target)
local path = PathfindingService:CreatePath(Params)
path:ComputeAsync(RootPart.Position , target.HumanoidRootPart.Position)
local waypoints = path:GetWaypoints()
if target and target.Parent and canSee(target) then
print("Chasing")
if target.Humanoid.Health > 0 and target and Players:FindFirstChild(target.Name) then
repeat
spPath:Run(target.HumanoidRootPart.Position)
NPC:SetAttribute("Chasing", true)
curTarget = target
local Magnitude = (RootPart.Position - target.HumanoidRootPart.Position).magnitude
if Magnitude < 3 then
jumpscare(target)
end
until target.Humanoid.Health < 1 or Magnitude > leaveDistance or NPC.Humanoid.Health < 1
print("Stopped Chasing")
if spPath and spPath._moveConnection then
spPath:Stop()
end
NPC:SetAttribute("Chasing", false)
curTarget = nil
end
end
end ```