Finding Children that don't exist (yet)

Hi, I’m creating a system that creates folders for players and stores values in them. However, I’m having trouble accessing it from other scripts.

This is a Script in ServerScriptService which works as planned:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local fireClientSetup = ReplicatedStorage:WaitForChild("fireClientSetup")

game.Players.PlayerAdded:Connect(function(p)
	print('[SERVER] Attempting player setup on ' .. p.Name .. ' [' .. p.UserId .. ']')
	
	-- Create Folder
	local playerFolder = Instance.new('Folder', game.ReplicatedStorage)
	playerFolder.Name = p.Name .. ' [' .. p.UserId .. ']'
	print('[SERVER] Folder successfully created for ' .. p.Name .. ' [' .. p.UserId .. ']')
	
	-- Create Player Settings
	local fovSetting = Instance.new("IntValue", playerFolder)
	fovSetting.Name = "fovSetting"
	fovSetting.Value = 90
	print('[SERVER] fovSetting successfully created for ' .. p.Name .. ' [' .. p.UserId .. ']')
	
	local sensitivitySetting = Instance.new("IntValue", playerFolder)
	sensitivitySetting.Name = "sensitivitySetting"
	sensitivitySetting.Value = 1
	print('[SERVER] sensitivitySetting successfully created for ' .. p.Name .. ' [' .. p.UserId .. ']')
	
	fireClientSetup:FireClient()
end)

Here, I’m trying to access playerFolder through a LocalScript inside StarterCharacterScripts:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local fireClientSetup = ReplicatedStorage:WaitForChild("fireClientSetup")

local p = game.Players.LocalPlayer

local playerFolder = ReplicatedStorage:FindFirstChildWhichIsA("Folder")

local function onClientEvent()
	wait(1)
	if playerFolder.Name == p.Name .. ' [' .. p.UserId .. ']' then
		print("[CLIENT] Folder found for " .. p.Name .. ' [' .. p.UserId .. ']')
	else
		print("[CLIENT] Error when attempting to find folder")
	end
end

fireClientSetup.OnClientEvent:Connect(onClientEvent)

This is the Developer Console:

image

Basically, if you’re still confused, I’m trying to access a folder that doesn’t exist until the script is run. I thought using FindFirstChildWhichIsA would work, but it hasn’t.

I’d like to know why it isn’t working and how I can fix it. Thanks! :slight_smile:

RemoteEvent:FireClient() requires player argument.

Try this instead :smiley:

fireClientSetup:FireClient(p) -- the p is the player that you've defined when player joins.

But when you are getting the triggered event from the client using event.onClientEvent then you dont need to mention the player in there.
Example

-- Server
 event:FireClient(player,"Hi!")

-- Client
  event.OnClientEvent:Connect(function(msg)
    print(msg) -- prints "Hi!"
  end)

Thanks for the paramater info…however it’s now not even finding the folder, even though it exists:

image

image

First just check if a folder is made by the player, then do FindFirstChildWhichIsA()

Try using find first child,
heres the client script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local fireClientSetup = ReplicatedStorage:WaitForChild("fireClientSetup")

local p = game.Players.LocalPlayer

local function onClientEvent()
	wait(1)
	if ReplicatedStorage:FindFirstChild(p.Name .. ' [' .. p.UserId .. ']')  then
		print("[CLIENT] Folder found for " .. p.Name .. ' [' .. p.UserId .. ']')
	else
		print("[CLIENT] Error when attempting to find folder")
	end
end

fireClientSetup.OnClientEvent:Connect(onClientEvent)
1 Like

Just use Parent:WaitForChild("Child name"). It will yield the script until there is a child called the child name, and then it will return the child.