Optimize lag from looped moving parts

Got a few questions. making a parkour map and have a set of rotating parts in a few levels maybe like 8-9 parts(each with its own scripts) its cause slight lag on my PC. its pretty high end meaning low end PC’s might lag even more which makes me sad.

what could be a way to reduce this reason of lag so I can further add more rotating parts.

my current rotating script is

while true do
script.Parent.CFrame = script.Parent.CFrame * CFrame.fromEulerAnglesXYZ(0,0.04,0)wait (0.02)
end

and in other moving its

while true do 
	wait()
	for i = 1,36 do
	script.Parent.CFrame = CFrame.new(script.Parent.Position + Vector3.new(-10,0,0))
	wait()
end

 for i = 1,36 do
	wait()
	script.Parent.CFrame = CFrame.new(script.Parent.Position - Vector3.new(-10,0,0))
	wait()
	end
	end

second question… all my kill parts are using its own scripts to kill the player… that I probably have over 100+ parts… at the moment I don’t think its causing lag, but as I progress and add plenty more levels I don’t want it to cause lag. is there a way to have one kill script parent all the bricks in various levels?

my kill script currently

function KillPlayer(part)
local h = part.Parent:findFirstChild("Humanoid")

	if h ~= nil then

		h.Health = 0

	end

end



script.Parent.Touched:connect(KillPlayer)
2 Likes

For the kill bricks, you could have them all named the same, and just do a loop through workspace, and connecting a touched event to them. Something like…

for i, v in pairs(workspace:GetChildren()) do
    if v.Name == "KillBrick" then
       v.Touched:Connect(function(hit)
           --Put the rest of the script here
       end)
    end
end

Then again, I’m not too sure how much more of a performance effect that way will have.

1 Like

where you put

–put the rest here

im guessing just copy/paste the kill player function ?

It’s probably the server-client delay. Are you using a server script? Try using a localscript to move them for each player. The killbrick script should be fine.

Yeah, you would just add that where I said put the rest here.

local h = hit.Parent:findFirstChild("Humanoid")

	if h ~= nil then

		h.Health = 0

	end

end
1 Like

I copied out the current script im using for moving parts
and local scripted them… no effect no idea why since its such a short script. while true doesn’t work locally?

while true do
script.Parent.CFrame = script.Parent.CFrame * CFrame.fromEulerAnglesXYZ(0,0.04,0)wait (0.02)
end

Well, since I see script.Parent, I’m assuming you just converted it to a local script from a server script. But local scripts need to be somewhere that replicates to the player, such as Starterplayerscripts.

wait what, yeah I changed it from a script to a local script located inside the part… if I move this local script into Starterplayer how would I connected it to its respected part?

script.game.workspace.level.part and then cFrame it?

Yeah, you need to change the path to where it is in the workspace.

I did path it to its respected part but still not getting any feedback in part
saying error
laser is not a valid member of local script? where did I mess up

local laser = game.Workspace.ObbyStructure.towerofhell.red.lazer

while true do

script.laser.CFrame = script.Parent.CFrame * CFrame.fromEulerAnglesXYZ(0,0.04,0)wait (0.02)

end

what Is this for? checkpoints? is this to solve team Skips?

Oof wrong page sorry [30 CHARS]

I will look this page 20 min later

ahahah and its alright thank you! if you could help me solve my checkpoint skips later that would be pretty epic though I made a post about it already on here but sadly no help

:smiley: sorry again [30 CHAAARSSSS]

Sorry for the late reply, but you need to do laser.CFrame = laser.CFrame and then do the * Eulerangles thing.

In the loop do:
laser.CFrame = laser.CFrame * CFrame.Angles(0, 0.04, 0) wait(0.02)
Also keep in mind that this will move the parts individually for each player. The server will only see a static part. So the killscript should also be local.

You could check if they are visible on the screen before rotating them : Camera | Documentation - Roblox Creator Hub, also group them either into a folder or using tags and just iterate over all of them using Folder:GetChildren() or CollectionService:GetTagged()

I have multiple moving parts with the same tag as ‘laser’, when I test this script only moves one of them.
could I use this one script to move em all?

You could reference in each script to the part you want or use one table that contains all parts and loop through that. The latter would be better in my opinion as you would only need one script.