I have a code that spawns a part between 2 points, its CFrame and Size is updated on RenderStepped (but does the same if changed to Heartbeat). When doing so, the game goes through extreme lag.
Something might be wrong cause this is a simple proccess and shouldnt lag at all. Any ideas or advise on this would be appreciated.
I clipped a video and a print of the long microprofiler tags that appear:
The important part of the code and how the line is made is here:
local far = Vector3.new(0,0,0)
local unusedLines = {}
local usedLines = {}
-- creates a part cache to get while scanning:
for i = 1, 20 do
local line = script.Line:Clone()
line.Parent = workspace
line.Position = far
table.insert(unusedLines, line)
end
-- variables to use on TweenLine function:
local lookat = CFrame.lookAt
local v3 = Vector3.new
local linefirst = true
local NeckMSGParticleAttachment = (game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()):WaitForChild("Head")
local RunService = game:GetService("RunService")
-- Line position function:
local function TweenLine(line, targetpos)
line.CFrame = lookat((NeckMSGParticleAttachment.Position + targetpos)/2, targetpos)
line.Size = v3(0.01,0.01,(NeckMSGParticleAttachment.Position - targetpos).Magnitude)
line.Sound:Play()
linefirst = false
RunService.RenderStepped:Connect(function(dt)
line.CFrame = lookat((NeckMSGParticleAttachment.Position + targetpos)/2, targetpos)
line.Size = v3(0.01,0.01,(NeckMSGParticleAttachment.Position - targetpos).Magnitude)
end)
end
TweenLine(unusedLines[1], v3(0,0,0))
help :c