How can I reduce this lag?

So I have a cart ride game and there is a feature where players can break your track, and I made a way to repair it, but I have already found lag.

I am only 1% done the track and it already freezes a bit when you repair the track.

the script pretty much destroys the current track, and replaces it with a fixed one.

local startclone = game.Workspace.Rails1:Clone()
startclone.Parent = game.ReplicatedStorage
local debounce = false

script.Parent.ProximityPrompt.TriggerEnded:Connect(function(player)
	local Tool = player.Backpack:FindFirstChild("RepairTool")  or player.Character:FindFirstChild("RepairTool")
	if Tool and debounce == false then
		debounce = true
		print("Fixing rails")
		game.Workspace.Rails1:Destroy()
		wait(3)
		local clone = game.ReplicatedStorage.Rails1:Clone()
		clone.Parent = game.Workspace
		Tool:Destroy()
		wait(27)
		debounce = false
	end
end)

I do have an idea to fix this, but I do not know how to make it actually happen. My idea is if I make the track repair in parts, so it is not all at once. The track is a folder, filled with models which are sections of the trap. Again, I do not know how I could make it replace one by one.

If you have a different idea that may work better, please let me know and if you know how to code my idea, then please tell me how.

1 Like

can you provide a video of the issue please

here is the video, also at the end I explain some things.
robloxapp-20221124-1426424.wmv (2.3 MB)
I know My walking is a bit laggy, but that is also because I have other things open. the main problem is the freezing, which will get worse as |I have explained in the video using the chat.

its because you are destroying and replacing the entire track of course it is gonna lag

1 Like

yeah, so I am trying to find a way to do something else but still repair the holes

Can you split the track into smaller segments, and just replace the one that the user is currently on?

Hello everyone.
First you can try resucing the part count present in the model. Second, try saving the model in Replicated storage, once the repair is called for in the script, you can clone it directly from the storage and insert it into the workspace.

The player has to repair at the workbench, so they are not one a track.

I can not make less parts as the track is meant to be destructible.
the clone is already there, It clones right when the server is created.