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

Help with :Clone()?

Asked by 10 years ago

Why won't the Guis clone in to the PlayersGui folder(Line 12)? I have no errors at all.

The script is setup like this.

while wait() do
    for _,Player in pairs (game.Players:GetChildren()) do
-------------------------------------------------------
        local Part = script.Parent
        local Character = Player.Character
        local Distance = (Character.Torso.Position - Part.Position).magnitude
        local HasGui = false
        ------------------------------------------------------- 
        print(Distance)
            if Distance < 4 then
                CopyGuis = coroutine.create(function()
                for _,Guis in pairs (Part.GuiFolder:GetChildren())do
                    CopiedGuis = Guis:Clone()
                        CopiedGuis.Parent = Player.PlayerGui
            FoundCopyGuis = Player.PlayerGui:FindFirstChild(CopiedGui)
                if FoundCopyGuis then
                        coroutine.yield(CopyGuis)
                    print("This player has all guis")
                else
                    coroutine.resume(CopyGuis)              

    end
        end
            end)
                end
                    end
                        end




1
**Please** tab correct BlueTaslem 18071 — 10y

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

Please tab your code correctly.


There is no reason at all here to use a coroutine.

In addition, your checks for making sure they only get one copy are set up too complicated and incorrectly.

In addition, FindFirstChild takes a string as its parameter, not an object. We ask for an object's Name when searching for it with FindFirstChild

The resulting script is simpler:

while wait() do
    for _,Player in pairs( game.Players:GetChildren() ) do
        -------------------------------------------------------
        local Part = script.Parent
        local Character = Player.Character
        local Distance = (Character.Torso.Position - Part.Position).magnitude
        local HasGui = false
        ------------------------------------------------------- 
        print(Distance)
        if Distance < 4 then
            for _,Gui in pairs (Part.GuiFolder:GetChildren())do
                Copy = Gui:Clone()
                FoundCopy = Player.PlayerGui:FindFirstChild(Copy.Name)
                if not FoundCopy then
                    Copy.Parent = Player.PlayerGui
                end
            end
        end
    end
end

I renamed some of your plural variables to singular to better reflect the fact that they are not collections of multiple things but just a thing in a collection.

Ad

Answer this question