Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

BodyPosition's ReachedTarget() event doesn't work?

Asked by 6 years ago

Hello there,

I was wondering why my BodyPosition's ReachedTarget() event never fires. It's in a local script, which is in StarterGui. This part is just a part that goes to where the mouse is pointing, which works but ReachedTarget() doesn't work.

Here is the code:

local plrs = game:GetService("Players");

local plr = plrs.LocalPlayer;

local uis = game:GetService("UserInputService");

local mouse = plr:GetMouse();

local debris = game:GetService("Debris");

function click(obj, gp)

    if obj.KeyCode == Enum.KeyCode.Q and not gp then
        local part = Instance.new("Part");

        local char = plr.Character or plr.CharacterAdded:Wait();

        local torso = char:FindFirstChild("Torso") or char:WaitForChild("UpperTorso");

        part.Shape = Enum.PartType.Ball;

        part.Size = Vector3.new(5, 5, 5);

        part.CanCollide = false;

        part.Transparency = .5;

        part.BrickColor = BrickColor:random();

        part.TopSurface = Enum.SurfaceType.Smooth;

        part.BottomSurface = Enum.SurfaceType.Smooth;

        part.Parent = workspace;

        part.Touched:Connect(function(hit)

            if hit.Parent.Name == "Part" and hit.Parent:IsA("Model") then

                hit:Destroy();

                part:Destroy();
            end
        end)

        local bv = Instance.new("BodyPosition");

        bv.Parent = part;

        bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge);

        local pos = mouse.Hit.p;

        local diff = (pos - torso.Position).magnitude;


        if diff > 50 then
            pos = pos.Unit * 50;
        end


        bv.Position = pos;


        part.CFrame = torso.CFrame;

        bv.ReachedTarget:Connect(function()
            print("It reached.");
        end)

        debris:AddItem(part, 5);
    end 
end

uis.InputBegan:Connect(click);

Any help is appreciated, thanks.

~~ KingLoneCat

Answer this question