How to get the new parent after cloning

I am attempting to clone a Folder that stores player values inside, but when I clone I cannot seem to get the new parent after cloning. It always returns the old parent as the current one. Any suggestions?

Where are you trying to clone it to? Did you set the new parent for the folder when cloning it?

local folder = ("whatever the folder you are indexing is at")
local clonedFolder = folder:Clone()
clonedFolder.Parent = ("whatever you want the cloned folders parent to be")
1 Like

I set the parent of the folder to another folder in server storage. Also how would I get the parent after the folder was cloned?

And are you trying to get the parent of that folder?

local getParent = clonedFolder.Parent -- this gets the parent of the cloned folder and makes it a variable

There’s a script inside the folder that decides if one of the values are changed, but when its cloned it gets the wrong parent. I am trying to get the parent of the scriptScreenshot_1

Do the folders have the same name by any chance? If both of the folders are under Server Storage with the same name, then the script will pick a folder at random or at times not even run because it doesn’t know which folder you are referencing.

If the folders have the same name (I’m assuming they do because it’s a clone) then set the name of the cloned folder to something different but easy to remember.

local clonedFolder = folderYoureCloning:Clone()
clonedFolder.Name = "Enter the new name here"

The folder Im cloning is inside of serverscriptservice; the clone is being put inside of server storage. When the folder gets cloned it changes the name of the folder to the name of the player. Im asking how to get the new parent from the script inside the folder after the clone.

Ah I see, you’re cloning the folder from ServerScriptService into ServerStorage, are you using the script inside the folder to clone it by any chance? Because if it’s cloning the entire folder then the script is being cloned along with it, which can cause confusion for the scripts. If this is the case, then I’d create a separate script that always stays on its own in ServerScriptService (not in a folder), and as for the getting the new parent it would be relatively simple, using the standalone script in ServerScriptService i recommended, you would do what I said from above just set the new clonedFolders Parent to a variable

local newParent = clonedFolder -- to get the new folder (aka the parent of where the script was)

or using the script inside the folder (not creating a standalone script in ServerScriptService), after it’s cloned (and you’re sure there is no confusion for what folder is what), then just add

local clonedParent
local is_clone

if script.Parent.Name ~= "theFolderYouClonedFrom" then
    local is_clone = true
end

wait()
if is_clone == true then
    clonedParent = script.Parent
end

This way, you can get the parent of the cloned folder using the cloned script inside that folder, thus getting the parent of the cloned script (aka the folder)

Argument 1 missing or nil


if script.Parent.Name ~= "Folder" then
     is_clone = true
end

wait()
if is_clone == true then
    clonedParent = script.Parent
end


script:FindFirstAncestor(clonedParent).EXP.Changed:connect(function()
	print "g"
	if script.Parent.EXP.Value == 50
	then script.Parent.Level.Value = script.Parent.Level.Value + 1
end
end)

Even if I didn’t use FindFirstAncestor it thinks I’m looking for a child of the script

Instead of using script:FindFIrstAncestor(clonedParent just use clonedParent since it was defined above in the is_cloned check.

So it would be

script:FindFirstAncestor(clonedParent).EXP.Changed:connect(function()
    print "g"
    if script.Parent.EXP.Value == 50 then 
        script.Parent.Level.Value = script.Parent.Level.Value + 1
    end
end)

Also instead of using .Changed try using :GetPropertyChangedSignal("Value") and make sure :connect is upperCase as the lower-case version has been depricated.

script:FindFirstAncestor(clonedParent).EXP:GetPropertyChangedSignal("Value"):Connect(function()
    print "g"
    if script.Parent.EXP.Value == 50 then 
        script.Parent.Level.Value = script.Parent.Level.Value + 1
    end
end)

[ServerScriptService.ForCloning.Folder.Script:17: attempt to index local ‘clonedParent’ (a nil value)]

Now it’s only running while its inside of the original folder. After it gets cloned, it doesn’t go through the script again.

Try this

local clonedParent
local is_clone = false

if script.Parent.Name ~= "the original folders name" then -- assuming the cloned folder has a different name (like Folder2)
    is_clone = true
end

wait()

if is_clone == true then
    clonedParent = script.Parent
end

clonedParent:WaitForChild("EXP"):GetPropertyChangedSignal("Value"):Connect(function()
    print("g")
    if clonedParent.EXP.Value == 50 then
        clonedParent.Level.Value = clonedParent.Level.Value + 1
    end
end)

If it still doesn’t work then it could be what I mentioned earlier about the scripts getting confused, at that point you’ll need to make a standalone script in ServerScriptService (not in the folder) that does this – I would highly recommend doing this.

image1

The parent of a cloned instance is nil until you set it.

1 Like

Still an error message of [ServerScriptService.ForCloning.Folder.Script:18: attempt to index local ‘clonedParent’ (a nil value)]
The script works fine if I don’t set the original folder name, but when I do set it to the original name it errors. Also I am using a seperate script to clone it. The StatValues script clones the folder on player entry.
Screenshot_2
If I don’t set the original name it only works for the original folder; not the cloned folder

I think the problem lies with the cloned script not re running after cloned

Are you trying to get the script to “re-run” after the cloning process? If so then putting it into ServerStorage might not work (not 100% positive) or are you trying to clone the folder to clone INTO a folder in ServerStorage? Then extracting it later?

I would still put the code from inside the folder into a script in ServerScriptService, into the StatValues script actually, that way you only have one script listening for changes in XP in all of the player folders, it’s way more efficient, and works way better.

The script is meant to level a player up after reaching a certain EXP level. In order for that to work it NEEDS to re run after being cloned. Sounds like that won’t work; any other ways I could accomplish this?

Yes

game.Players.PlayerAdded:Connect(function(player)
    local Folder = game:GetService("ServerScriptService"):WaitForChild("ForCloning").Folder
    local clonedFolder = Folder:Clone()
    clonedFolder.Parent = game:GetService("ServerStorage")
    clonedFolder.Name = player.Name
    
    local EXP = clonedFolder:WaitForChild("EXP")
    local Level = clonedFolder:WaitForChild("Level")
    
    EXP:GetPropertyChangedSignal("Value"):Connect(function()
        if EXP.Value == 50 then
            Level.Value = Level.Value + 1
        end
    end)
end)

Adapt this script however you like to match with the StatValues script, thats where the code should be, not in each separate folder (so this code runs each time a player joins and throughout the game)

3 Likes