Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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.

-- variables
local uis = game:GetService("UserInputService")
local cam = game.Workspace.CurrentCamera
local runService = game:GetService("RunService")
local connection = nil
local part = Instance.new("Part")
part.Parent = workspace
local alignPos
local alignRot
local attch

-- returns mouse location in 3d space
local function getMouse()
	local x = uis:GetMouseLocation().X
	local y = uis:GetMouseLocation().Y
	print(1)
	local camRay = cam:ViewportPointToRay(x, y, 1)
	return camRay
end

-- part manipulation function
local function followMouse(target, alignPos)
	connection = runService.Heartbeat:Connect(function()
		local camRay = getMouse()
		alignPos.Position = (camRay.Origin + camRay.Direction * 10)
	end)
end

-- verifying input, part, and mouse ray validity
uis.InputBegan:Connect(function(input)
	if input.UserInputType ~= Enum.UserInputType.MouseButton1 then return end
	local camRay = getMouse()
	local rayParams = RaycastParams.new()
	rayParams.FilterDescendantsInstances = {game.Players.LocalPlayer.Character}
	rayParams.FilterType = Enum.RaycastFilterType.Blacklist
	local rayCast = workspace:Raycast(camRay.Origin, camRay.Direction * 1000)
	if not rayCast then return end
	local target = rayCast.Instance
	if (target.Position - game.Players.LocalPlayer.Character.HumanoidRootPart.Position).magnitude > 15 then return end
	-- constraint creation
	alignPos = Instance.new("AlignPosition")
	attch = Instance.new("Attachment")
	alignRot = Instance.new("AlignOrientation")
	alignRot.Parent = target
	alignRot.Mode = Enum.OrientationAlignmentMode.OneAttachment
	alignRot.Attachment0 = attch
	alignPos.Parent = target
	attch.Parent = target
	alignPos.Attachment0 = attch
	alignPos.Mode = Enum.PositionAlignmentMode.OneAttachment
	-- calling part manipulation function
	followMouse(target, alignPos, alignRot)
end)

-- disconnects and destroys heartBeat and constraints
uis.InputEnded:Connect(function(input)
	if input.UserInputType ~= Enum.UserInputType.MouseButton1 then return end
	if not connection then return end
	connection:Disconnect()
	alignPos:Destroy()
	alignRot:Destroy()
	attch:Destroy()
	connection = nil
end)

1 answer

✓ Accepted answer

Roblox studio loses fps when it has a lot of stuff in the output console for some reason. I suggest you try removing the print(1) in the getMouse() function

1 comment

0

It works thank you, strange ig roblox stores each print function as a variable instead of overriting it.

This discussion is preserved as it was. New answers, comments, and votes are closed.