So this script creates a folder inside of another folder called PickedGun. The created folder is named after the person who joins. This script is in a server script in workspace.
game.Players.PlayerAdded:Connect(function(p)
local folder = Instance.new("Folder",workspace.PickedGun)
folder.Name = p.Name
end)
game.Players.PlayerRemoving:Connect(function(p)
workspace.PickedGun:WaitForChild(p.Name):Destroy()
end)
I have a script that puts a viewmodel into the PickedGun folder, but I want the viewmodel to be parented to the folder that is created by the previous script. Here is the line of code that parents the viewmodel to the PickedGun folder, I've tried to modify it to fit my needs but it hasn't worked. This is the protion of the localscript inside of StarterGUI
ViewModel.Parent = workspace.PickedGun:GetChildren(name)
and I tried to define "name" here
local p = workspace.PickedGun:GetDescendants()
local name = workspace.PickedGun:FindFirstChild(p.name)
This is very elaborate, tldr is that I need the viewmodel to be parented by a folder that only is added when a player joins.
The viewmodel is transported to the "PickedGun" folder because the localscript clones it and parents it to workspace.PickedGun

15 comments
No, this won't work because the pickedgun folder doesn't have any children until a new player joins. The first script created the folder and named it after the player who joined, so I want the viewmodel to be parented by the created folder.
Parented by the created folder? So player joins, your script creates a subfolder. At some point when the player is in the game, a "ViewModel" has to be put into the subfolder and you want to know how to parent it to that subfolder?
Yes, I want there to be a delay for when the viewmodel is added. Maybe about 5 seconds, or it happens instantly like it is right now.
wait(5)?
Yes, but when I do that, it seems to do nothing. Also this is the line of code that adds that is supposed to make the viewmodel a child of the subfolder: ViewModel.Parent = workspace.PickedGun:GetChildren(name). I also tried defining the "name" var.
But forget about the wait, how would I make the parent of the viewmodel a folder that doesn't yet exist? do I delay the script? Edit your answer to help me out,
https://developer.roblox.com/en-us/api-reference/function/Instance/GetChildren GetChildren doesn't have a name parameter. Just use FindFirstChild(name) instead
To wait for it to exist, use :WaitForChild(name)
Oh!
So what should I define the child as in PickedGun:WaitForChild(?) ?
PickedGun:WaitForChild(Player.Name) - and that will be the subfolder in PickedGun named after the Player. If you want to find the viewmodel as well, just put a :WaitForChild('ViewModel') after it.
If it's in a LocalScript on the client, you can use: local Player = game.Players.LocalPlayer to get the Player object.
Thanks, I'll try it tomorrow morning because I already closed studio and I'm too tired right now. I'll mark as a solution after I test it. But even if it doesn't work, which at this point I doubt, thanks for your effort.
No worries.
Worked like a charm