runService.HeartBeat slowing down part movement script?
Hello I am making a script that allows the player to manipulate position and rotation of a part, I have this working fine how ever, when using a heartbeat event my fps / lag will get progressively worse the longer I am manipulating the part. At about the 3000th iteration it becomes really noticeable. Any suggestions or solutions would be appreciated. this script I've dumbed down can be copy / pasted straight into a local script inside the player to test.
02 | local uis = game:GetService( "UserInputService" ) |
03 | local cam = game.Workspace.CurrentCamera |
04 | local runService = game:GetService( "RunService" ) |
06 | local part = Instance.new( "Part" ) |
07 | part.Parent = workspace |
13 | local function getMouse() |
14 | local x = uis:GetMouseLocation().X |
15 | local y = uis:GetMouseLocation().Y |
17 | local camRay = cam:ViewportPointToRay(x, y, 1 ) |
22 | local function followMouse(target, alignPos) |
23 | connection = runService.Heartbeat:Connect( function () |
24 | local camRay = getMouse() |
25 | alignPos.Position = (camRay.Origin + camRay.Direction * 10 ) |
30 | uis.InputBegan:Connect( function (input) |
31 | if input.UserInputType ~ = Enum.UserInputType.MouseButton 1 then return end |
32 | local camRay = getMouse() |
33 | local rayParams = RaycastParams.new() |
34 | rayParams.FilterDescendantsInstances = { game.Players.LocalPlayer.Character } |
35 | rayParams.FilterType = Enum.RaycastFilterType.Blacklist |
36 | local rayCast = workspace:Raycast(camRay.Origin, camRay.Direction * 1000 ) |
37 | if not rayCast then return end |
38 | local target = rayCast.Instance |
39 | if (target.Position - game.Players.LocalPlayer.Character.HumanoidRootPart.Position).magnitude > 15 then return end |
41 | alignPos = Instance.new( "AlignPosition" ) |
42 | attch = Instance.new( "Attachment" ) |
43 | alignRot = Instance.new( "AlignOrientation" ) |
44 | alignRot.Parent = target |
45 | alignRot.Mode = Enum.OrientationAlignmentMode.OneAttachment |
46 | alignRot.Attachment 0 = attch |
47 | alignPos.Parent = target |
49 | alignPos.Attachment 0 = attch |
50 | alignPos.Mode = Enum.PositionAlignmentMode.OneAttachment |
52 | followMouse(target, alignPos, alignRot) |
56 | uis.InputEnded:Connect( function (input) |
57 | if input.UserInputType ~ = Enum.UserInputType.MouseButton 1 then return end |
58 | if not connection then return end |
59 | connection:Disconnect() |