Script do without making delay on next lines

Hey, i want to make so this entire script do without making delay on next lines?, can somebody help. it isnt working!

for _, v in pairs(game.ReplicatedStorage.Items.PlateCosmetics:GetChildren()) do
						if v.Name == player.PlateCosmeticEquippedSlot1.Value then
							local comseticclone = v:Clone()
							comseticclone.Parent = plate
							task.spawn(function()
								while game.ReplicatedStorage.Round.Value == true do
									comseticclone:SetPrimaryPartCFrame(plate.Part.CFrame + Vector3.new(0,1,0))
								end
							end)
							
						elseif v.Name == player.PlateCosmeticEquippedSlot2.Value then
							local comseticclone = v:Clone()
							comseticclone.Parent = plate
							task.spawn(function()
								while game.ReplicatedStorage.Round.Value == true do
									comseticclone:SetPrimaryPartCFrame(plate.Part.CFrame + Vector3.new(0,1,0))
								end
							end)
						elseif v.Name == player.PlateCosmeticEquippedSlot3.Value then
							local comseticclone = v:Clone()
							comseticclone.Parent = plate
							task.spawn(function()
								while game.ReplicatedStorage.Round.Value == true do
									comseticclone:SetPrimaryPartCFrame(plate.Part.CFrame + Vector3.new(0,1,0))
								end
							end)
						end
					end

sorry if this code is messy!

2 Likes

What are you looking for it to not delay? I don’t see anything wrong with your code.

I want to update a specific part of my script to avoid any pauses or delays caused by loops or waits while keeping the code running smoothly.

I haven’t learned how to use any of these so I can’t give any examples but I hope there’s tutorials on youtube.

task.defer might be the way to do this.

Look into Coroutines. They help a lot in these cases, if I understand your problem.

Basically, they allow you to run code on a different thread, aka at the (almost) same time as other code in the same script.
I’ll show you how to use coroutine.wrap() in this case:

coroutine.wrap(function(
for _, v in pairs(game.ReplicatedStorage.Items.PlateCosmetics:GetChildren()) do
						if v.Name == player.PlateCosmeticEquippedSlot1.Value then
							local comseticclone = v:Clone()
							comseticclone.Parent = plate
							task.spawn(function()
								while game.ReplicatedStorage.Round.Value == true do
									comseticclone:SetPrimaryPartCFrame(plate.Part.CFrame + Vector3.new(0,1,0))
								end
							end)
							
						elseif v.Name == player.PlateCosmeticEquippedSlot2.Value then
							local comseticclone = v:Clone()
							comseticclone.Parent = plate
							task.spawn(function()
								while game.ReplicatedStorage.Round.Value == true do
									comseticclone:SetPrimaryPartCFrame(plate.Part.CFrame + Vector3.new(0,1,0))
								end
							end)
						elseif v.Name == player.PlateCosmeticEquippedSlot3.Value then
							local comseticclone = v:Clone()
							comseticclone.Parent = plate
							task.spawn(function()
								while game.ReplicatedStorage.Round.Value == true do
									comseticclone:SetPrimaryPartCFrame(plate.Part.CFrame + Vector3.new(0,1,0))
								end
							end)
						end
					end

))()
1 Like