Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make a folder that is created after a player joins the parent of a viewmodel?

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

1 answer

✓ Accepted answer

:GetChildren() returns a table containing all the "children" objects of the instance you are running the function on.

:GetDescendants returns a table containing all the descendant objects of the instance your are running the function on.

To parent your "ViewModel" to the Player's subfolder inside of "PickedGun", do:

ViewModel.Parent = workspace.PickedGun:WaitForChild(Player.Name)

To find it at a later time, just use:

local ViewModel = workspace.PickedGun[Player.Name]:FindFirstChild('ViewModel')

Or:

local ViewModels = workspace.PickedGun[Player.Name]:GetChildren()
 -- ViewModels is a table of all the objects inside of the player's subfolder

If you decide to use the subfolder method, and you only expect there to be one object inside of said subfolder, but don't know it's name, you can use:

local ViewModel = workspace.PickedGun[Player.Name]:GetChildren()[1]
 -- ViewModel is the first object indexed in the :GetChildren() table

15 comments

0

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.

0

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?

0

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.

0

wait(5)?

0

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.

0

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,

0

https://developer.roblox.com/en-us/api-reference/function/Instance/GetChildren GetChildren doesn't have a name parameter. Just use FindFirstChild(name) instead

0

To wait for it to exist, use :WaitForChild(name)

0

Oh!

0

So what should I define the child as in PickedGun:WaitForChild(?) ?

0

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.

0

If it's in a LocalScript on the client, you can use: local Player = game.Players.LocalPlayer to get the Player object.

0

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.

0

No worries.

0

Worked like a charm

This discussion is preserved as it was. New answers, comments, and votes are closed.