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

Help with this script duplicating more than once?

Asked by
FiredDusk 1466 Moderation Voter
7 years ago

I am scripting a building kit and when I press the 'Refresh' button, it supposse to remove all parts in it that have a name of 'Part' and 'Thruss'. Then it suppose to make new parts in it but it is duplicating the parts more than once ;(

--//Services
local RS = game:GetService('ReplicatedStorage')

--//Variables
local Model = script.Parent.Parent
local CD = Model.Refresh.ClickDetector
local Clicked = false

--//Main
CD.MouseClick:connect(function()
    if Clicked == false then
        Clicked = true
        for i,v in pairs(Model:GetChildren()) do
            if v.Name == 'Part' or v.Name == 'Truss' then
                v:Destroy()
                print'1'
                local CloneModel = RS:WaitForChild('BuildingKit') --Make sure the model is in ReplicatedStorage
                for i,x in pairs(CloneModel:GetChildren()) do
                    if x.Name == 'Part' or v.Name == 'Truss' then
                        x:Clone().Parent = Model
                        print'2'
                    end
                end
            end
        end
        wait(5)
        Clicked = false
    end
end)

0
Btw, this crashes my studio, guessing due to many parts being created FiredDusk 1466 — 7y
1
line 19, you have x.Name == 'Part' , v.Name == 'Truss" is the x and v on purpose?  RobloxianDestory 262 — 7y

1 answer

Log in to vote
2
Answered by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
7 years ago
Edited 7 years ago

The loop that you're using to duplicate the parts is inside of the loop that you're using to delete parts. All you have to do is put it after that loop:

--//Services
local RS = game:GetService('ReplicatedStorage')

--//Variables
local Model = script.Parent.Parent
local CD = Model.Refresh.ClickDetector
local Clicked = false
local CloneModel = RS:WaitForChild('BuildingKit') --// No need to get the model again every time; just get it once.

--//Main
CD.MouseClick:connect(function()
    if Clicked == false then
        Clicked = true
        for i,v in pairs(Model:GetChildren()) do
            if v.Name == 'Part' or v.Name == 'Truss' then
                v:Destroy()
                print'1'
            end
        end
        for i,x in pairs(CloneModel:GetChildren()) do
            if x.Name == 'Part' or x.Name == 'Truss' then
                x:Clone().Parent = Model
                print'2'
            end
        end
        wait(5)
        Clicked = false
    end
end)

Hope this helped.

EDIT: I had already fixed it, but I forgot to note that you were also using v when you should have used x on line 19. Thanks, RobloxianDestory, for pointing that out.

1
you beat me to it. I was testing it when you answered it RobloxianDestory 262 — 7y
Ad

Answer this question