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

Object BodyPosition just stops working sometimes?

Asked by
BiIinear 104
4 years ago

Whenever I test my game in either studio or in-game, and I pick up an object, I notice that it just sometimes flops on the ground and takes a few seconds to get back in position. Ever since 2 weeks ago, my internet kinda went bad and the problem started to happen. But, people have the same issue regardless of connection.

The script displayed below is a LocalScript parented to StarterCharacterScripts.

--DragObject
InputService.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.E then
        if DragTarget == nil and Mouse.Target ~= nil then
            local t = Mouse.Target
            if t and t.Parent and t.Anchored == false and t.Size.magnitude <= 40 and (Head.Position-t.Position).magnitude <= DragMinDistance+t.Size.magnitude/2 and t:FindFirstChild("Object") and itemequipped.Value == false then
                if t:FindFirstChild("Object").Value > 0 then
                    DragTarget = t

                    local bp = Instance.new("BodyPosition", DragTarget)bp.Name = "DragPosition"
                    bp.MaxForce = Vector3.new(8000,8000,8000)
                    bp.P = 5500
                    bp.D = 400
                    Mouse.TargetFilter = DragTarget
                    DragTargetDistance = (Head.CFrame.p-DragTarget.Position).magnitude
                    bp.Position = (Head.Position + (Head.CFrame.p - Mouse.Hit.p).unit*-8)
                    coroutine.wrap(function()
                        while true do wait()
                            if DragTarget ~= nil then
                                dragMove()
                            else break
                            end
                        end
                    end)()
                end
            end
        elseif DragTarget ~= nil then
            DragTarget.Velocity = Vector3.new(0,0,0)
            DragTarget.DragPosition:Destroy()
            Mouse.TargetFilter = nil
            DragTarget = nil
        end
    end
end)

function dragMove()
        DragTargetDistance = (Head.CFrame.p-DragTarget.Position).magnitude/2
        DragTarget.DragPosition.Position = (Head.Position + (Head.CFrame.p - Mouse.Hit.p).unit*-6)
    end
end

NOTE: The BodyPosition flopping mostly happens when a lot of physics is happening, I'm sure BodyPosition relies on physics. If this is the reason why, then I'm not sure what to do. This is kind of a physics based game.

If anybody knows any solutions, that'd be greatly appreciated. This script is a vital part of my project.

0
Ignore the extra "end" in line 40, I forgot to remove that when editing the script before posting. BiIinear 104 — 4y

Answer this question