Hello! I have an issue with my script, where my while loop isn’t detecting the magnitude of my humanoidrootpart to my target part.
Here’s my script (LONG)
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local char = plr.Character
local hrp = char:WaitForChild("HumanoidRootPart")
local ammo = 1
local canActivate = true
local targetInstance = nil
script.Parent.Activated:Connect(function()
if ammo > 0 and canActivate then
canActivate = false
ammo -= 1
print("Activated Grappler")
mouset = mouse.Target
local mousePos = mouse.Hit
local yay, nay = pcall(function()
if mouset and mouset:IsA("BasePart") and
(hrp.Position - mouset.Position).Magnitude < 350 and
mouset.CanCollide then
local goalPart = Instance.new("Part")
goalPart.Parent = game.Workspace
goalPart.Anchored = true
goalPart.CanCollide = false
goalPart.Size = Vector3.new(4, 4, 4)
goalPart.BrickColor = BrickColor.new("Really red")
goalPart.Transparency = 0.5
goalPart.CFrame = mousePos
local StartPart = Instance.new("Part")
StartPart.Parent = game.Workspace
StartPart.Anchored = true
StartPart.CanCollide = false
StartPart.Size = Vector3.new(4, 4, 4)
StartPart.BrickColor = BrickColor.new("Lime green")
StartPart.Transparency = 0.5
StartPart.CFrame = hrp.CFrame
game.Debris:AddItem(StartPart, 10)
game.Debris:AddItem(goalPart, 10)
bv = Instance.new("BodyVelocity")
bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
bv.P = 0
bv.Parent = hrp
game.Debris:AddItem(bv, 10)
script["Grappling Gun Fire"]:Play()
targetInstance = mouset
bv.Velocity = mousePos.LookVector * 100
-- bv:Destroy()
task.wait(2)
canActivate = true
else
print("Grappling Hook requirements did not meet.")
canActivate = true
end
end)
else
print("Out of ammo!")
end
end)
hrp.Touched:Connect(function(goal)
if targetInstance and bv and goal == targetInstance then
bv:Destroy()
targetInstance = nil
-- targetInstance = nil
else
print("Part interrupt")
end
ammo = 1
end)
while task.wait() do
if targetInstance and bv then
if (hrp.Position - mouset.Position).Magnitude < 15 then
print("pos thing")
bv:Destroy()
targetInstance = nil
end
elseif not targetInstance then
print("No target instance")
elseif not bv then
print("No bodyvelocity")
else
print("idk")
end
end
(sorry I couldn’t use minimal reproducible example, I don’t know what is causing this issue.)
At the end of the script, you can find the while loop.
while task.wait() do
if targetInstance and bv then
if (hrp.Position - mouset.Position).Magnitude < 15 then
print("pos thing")
bv:Destroy()
targetInstance = nil
end
elseif not targetInstance then
print("No target instance")
elseif not bv then
print("No bodyvelocity")
else
print("idk")
end
end
It won’t detect the magnitude for whatever reason. It never prints “pos thing”.
I don’t know what is going on here, probably some dumb logic thing I couldn’t find myself, but the issue doesn’t seem obvious to me. Thank you, reader!