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))
I can’t assist in why it might lag in one place and not another, but I really have to question why you are using Part and RenderStepped, when a Beam would appear to do exactly the same function.
I agree with @BadDad2004 you should NOT be using a part to do this.
You are essentially forcing the game to re-render the part on every single frame update. Just use a beam, it does the same thing, with more customization and less lag.
Thanks for your advises, i discovered the problem. What i found out is pretty unconventional.
After hours of testing to understand what could be the problem with the map, my team found out what was the the problem:
There was a mesh with layered clothing on the workspace, not parented to a character.
This unique mesh was causing the whole game to be so lagged thats unplayable.
We were considering not continuing our project due to the lag issue.
It was a build made as a joke of some poped pants in the ground of the shower area.