Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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

01game.Workspace.Spart.ClickDetector.MouseClick:Connect(function()
02    UIS.InputBegan:Connect(function(Input,gp)
03        if Input.KeyCode == Enum.KeyCode.E and not gp then
04            if not en then return end
05            en = false
06                                    for i,v in pairs(char:GetChildren()) do
07                if i:IsA("Accessory") then
08                    local BackupAc = v:Clone()
09            game.ReplicatedStorage.SRemote3:FireServer(hum,root,Mouse.hit,SCircle,char,BackupAc)
10            wait(0.1)
11            char["Left Arm"].Transparency = 0
12            char["Right Arm"].Transparency = 0
13            char["Torso"].Transparency = 0
14            char["Head"].Transparency = 0
15            char["Left Leg"].Transparency = 0
View all 27 lines...

Server Script

01game.ReplicatedStorage.SRemote3.OnServerEvent:Connect(function(Player,hum,root,Mousehit,SCircle,char,Backup)
02    print(char)
03    local tweenInfo3 = TweenInfo.new(0.6,Enum.EasingStyle.Quad,Enum.EasingDirection.Out,0,false)
04    local tweenInfo4 = TweenInfo.new(1.5,Enum.EasingStyle.Quad,Enum.EasingDirection.Out,0,false)
05                char["Left Arm"].Transparency = 1
06            char["Right Arm"].Transparency = 1
07            char["Torso"].Transparency = 1
08            char["Head"].Transparency = 1
09            char["Left Leg"].Transparency = 1
10            char["Right Leg"].Transparency = 1
11            local SCircle = game.ReplicatedStorage.Spectre:Clone()
12            game:GetService("Debris"):AddItem(SCircle,2)
13            SCircle.Parent = workspace
14            SCircle.Anchored = true
15            SCircle.CanCollide = false
View all 46 lines...

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

Regards, Bl_ueHistory

0
Generic for loops can traverse a data structure. Tables. The body will execute for every element in a table. User#24403 69 — 5y
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. Bl_ueHistory 94 — 5y
0
Wanting only one iteration is stupid. You only want to iterate over one element of the whole table???? User#24403 69 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

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)

0
Thanks alot! Bl_ueHistory 94 — 5y
Ad

Answer this question