Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I make the for loop "for i,v in pairs(char:GetChildren())" do only happen once?

Backstory So for loops can be used for many things either that being getting the children of a object or making and event happening a certain amount of times. For me I'm using a for loop to get the children in a object that i will turn its transparency to 1. This script will then will be sent to the server using FE.

Problem So my actual problem is this. How can i make the for loops for i,v in pairs(char:GetChildren()) do only happen once. This script is playing mutiple times and then stopping when the player has for looped v enough times. Thank you!

Local Script

game.Workspace.Spart.ClickDetector.MouseClick:Connect(function()
	UIS.InputBegan:Connect(function(Input,gp)
		if Input.KeyCode == Enum.KeyCode.E and not gp then
			if not en then return end
			en = false
									for i,v in pairs(char:GetChildren()) do
				if i:IsA("Accessory") then
					local BackupAc = v:Clone()
			game.ReplicatedStorage.SRemote3:FireServer(hum,root,Mouse.hit,SCircle,char,BackupAc)
			wait(0.1)
			char["Left Arm"].Transparency = 0
			char["Right Arm"].Transparency = 0
			char["Torso"].Transparency = 0
			char["Head"].Transparency = 0
			char["Left Leg"].Transparency = 0
			char["Right Leg"].Transparency = 0
					BackupAc.Parent = char
					char.SpectreKnoife.Transparency = 0
					char.Head.face.Transparency = 0
					char.SpectreKnoife.Trail.Transparency = NumberSequence.new(0)
			wait(6)
			en = true
			end
						end
						end
	end)
	end)

Server Script

game.ReplicatedStorage.SRemote3.OnServerEvent:Connect(function(Player,hum,root,Mousehit,SCircle,char,Backup)
	print(char)
	local tweenInfo3 = TweenInfo.new(0.6,Enum.EasingStyle.Quad,Enum.EasingDirection.Out,0,false)
	local tweenInfo4 = TweenInfo.new(1.5,Enum.EasingStyle.Quad,Enum.EasingDirection.Out,0,false)
				char["Left Arm"].Transparency = 1
			char["Right Arm"].Transparency = 1
			char["Torso"].Transparency = 1
			char["Head"].Transparency = 1
			char["Left Leg"].Transparency = 1
			char["Right Leg"].Transparency = 1
			local SCircle = game.ReplicatedStorage.Spectre:Clone()
			game:GetService("Debris"):AddItem(SCircle,2)
			SCircle.Parent = workspace
			SCircle.Anchored = true
			SCircle.CanCollide = false
			--(Bottom = Bottom and Top = Top
				SCircle.Bottom.Face = "Bottom"
				SCircle.Top.Face = "Top"
			SCircle.CFrame = root.CFrame * CFrame.new(0,-2,0)
			local tween3 = TweenService:Create(SCircle,tweenInfo3,{Size = Vector3.new(12.32, 0.05, 18.84)})
			local tween4 = TweenService:Create(SCircle.Top,tweenInfo3,{Transparency = 1})
			local tween5 = TweenService:Create(SCircle.Bottom,tweenInfo4,{Transparency = 1})
			tween3:Play()
			tween4:Play()
			tween5:Play()
			for i, v in pairs(char:GetChildren()) do
				if v:IsA("Accessory") then
					local BackupAc = v:Clone()
					v:Destroy()
					Part2.Transparency = 1
					char.Head.face.Transparency = 1
					char.SpectreKnoife.Trail.Transparency = NumberSequence.new(1)
					wait(6)
								char["Left Arm"].Transparency = 0
			char["Right Arm"].Transparency = 0
			char["Torso"].Transparency = 0
			char["Head"].Transparency = 0
			char["Left Leg"].Transparency = 0
			char["Right Leg"].Transparency = 0
					BackupAc.Parent = char
					char.SpectreKnoife.Transparency = 0
					char.Head.face.Transparency = 0
					char.SpectreKnoife.Trail.Transparency = NumberSequence.new(0)
				end
			end
end)

Try not to pay attention to the actual script but the problem because the script is quite long.

Regards, Bl_ueHistory

3 comments

0

Generic for loops can traverse a data structure. Tables. The body will execute for every element in a table.

0

Yeah thanks for stating the obvious. Could you actually give me an answer to the problem instead of stating when the problem is? Thanks.

0

Wanting only one iteration is stupid. You only want to iterate over one element of the whole table????

1 answer

✓ Accepted answer

If you want to stop after the first Accessory you find, you can use break to exit the loop early.

If you want to apply your effects to all Accessories simultaneously, you must avoid using wait inside the for loop and split up your effects into multiple for loops if needed. In your case I expect you want to run lines 33 - 43 only once, after destroying all the accessories -- simply move them outside the for loop to fix that. You'll want to do the same sort of thing for the LocalScript, too.

Minor note: I expect line 7 is erroring in the LocalScript because i is an integer (you meant v:IsA not i:IsA)

1 comment

0

Thanks alot!

This discussion is preserved as it was. New answers, comments, and votes are closed.