How to change parent of child

I am trying to make a script that will automatically change the parent of everything that gets inside of a folder in Replicated Storage for my game. In the game, once you buy a house it gets sent to a folder named “boughtHouses”. After that, player can “equip” the house he wants to spawn, this will move the model of a house from “boughtHouses” to “cloneableHouses”.
Here we get to what I need help with. I have a button on my map that you can press to spawn a house on your land, I want that button to just keep sending everything from “cloneableHouses” to game.Workspace after cloning it, without me having to name what parts to move from that folder specifically.
I did a lot of research but I can’t seem to find anything that could help me with this so far.
This is how the code looks so far, you can see that the issue is that I have to name everything I want to change the .Parent of. I am very new to scripting and this is my first post so sorry if it’s messy.

local button = script.Parent

function onClicked()
    game.ReplicatedStorage.House1Storage.cloneableHouses.House1Spawner:clone()
	game.ReplicatedStorage.House1Storage.cloneableHouses.House1Spawner.Parent = game.Workspace              --is there a way to make everything in "cloneableHouses" automatically change parent to game.Workspace?
end


button.ClickDetector.MouseClick:connect(onClicked)
2 Likes

Fixed:


local button = script.Parent

function onClicked()
    local ClonedHouse = game.ReplicatedStorage.House1Storage.cloneableHouses.House1Spawner:clone()
	ClonedHouse.Parent  =  game.Workspace
    ClonedHouse.Name = "ClonedHouse"

end


button.ClickDetector.MouseClick:connect(onClicked)

When you are creating a clone, you need to assign a variable to it. In this case, I named the variable ClonedHouse

1 Like
local button = script.Parent

function onClicked()
	local house1Clone = game.ReplicatedStorage:WaitForChild("House1Storage"):WaitForChild("cloneableHouses"):WaitForChild("House1Spawner"):clone()
	house1Clone.Parent = game.Workspace
end

button.ClickDetector.MouseClick:connect(onClicked)

When you’re cloning an instance you need to assign the clone to a variable so that it can be referenced, I’ve also added :WaitForChild() for each instance inside ReplicatedStorage that is being used, this makes sure the instance has loaded before the script attempts to execute.

Cloning isn’t the problem, I got that working, the problem is that I want to change .Parent of every child of the game.ReplicatedStorage.House1Storage.cloneableHouses

So as soon as any part enters “game.ReplicatedStorage.House1Storage.cloneableHouses” I want the script to change that parts parent to game.Workspace

Is it possible to do it with :GetChildren() and then making it so the children change .Parent?

you can do it by using for loops. like this,

local model = game.ReplicatedStorage.House1Storage.cloneableHouses

for _, child in pairs(model:GetChildren()) do
    child.Parent = workspace
end

this basically goes through all child and then parent them to workspace.

3 Likes

This seems like it should work and it is exactly what I was looking for, but it doesn’t send them to workspace, I don’t really know why…

try game.Workspace instead of just workspace

1 Like

Yeah I did that already, I checked that the variables are all locating the right folders and everything, I don’t even get any errors, but still it won’t send it to workspace, imma try using your code in different ways to see if I can make it work somehow

it is still the same thing. game.Workspace or workspace.

1 Like

Yup, I know that. The code @TheLordGamer1820 sent should work so I will mark it as a solution, thanks for all the help everyone :slight_smile:

Would’ve been nice if you had just asked for what you needed from the get-go.

local button = script.Parent

function onClicked()
    game.ReplicatedStorage.House1Storage.cloneableHouses.House1Spawner:clone()
	game.ReplicatedStorage.House1Storage.cloneableHouses.House1Spawner.Parent = game.Workspace              --is there a way to make everything in "cloneableHouses" automatically change parent to game.Workspace?
end


button.ClickDetector.MouseClick:connect(onClicked)

This wouldn’t have worked by the way, since you were moving the original model to the workspace, meaning that if the ClickDetector was clicked again you’d get an error since the model is no longer located in ReplicatedStorage.

1 Like

I just got it to work, I was using a click detector on my button in a 3D space, all I had to do was make a UI on the screen that when pressed activates a local script with the code that @TheLordGamer1820 wrote. It does everything just like I wanted when combined with the cloning script that you helped me with : )
Now the player can buy multiple different houses, equip them and spawn.
Thank you all a ton!! : D

hello again, instead of using an invisible UI to see if a player clicks, try using UserInputService, it’s better and more efficient

1 Like


Maybe I said it wrong, I started programming a couple of weeks ago lol, I made a GUI spawn button that when clicked spawns the equipped house on the players’ plot. Thanks for the tip tho, I will definitely learn about UserInputService next.

oh okay, good luck in your game!

1 Like