Any way to access PlayerScripts, using FindFirstAncestor?

Title would be too long if it was descriptive enough, so here is the full story:
Is there a way to access PlayerScripts, from a LocalScript that is located in PlayerScripts, using something similar to FindFirstAncestor, but also supports autocomplete?

This removes autocomplete, since in studio its StarterPlayerScripts:

local playerScripts = script:FindFirstAncestor("PlayerScripts")

This does the same thing, because PlayerScripts does NOT inherit StarterPlayerScripts, or vice versa:

local playerScripts = script:FindFirstAncestorOfClass("PlayerScripts")
local playerScripts = script:FindFirstAncestorWhichIsA("PlayerScripts")

And yes, I am aware you can just do this:

local playerScripts = script.Parent

But in nested scripts, it just starts looking ugly:

local playerScripts = script.Parent.Parent.Parent.Parent

Is there anything that would work AND look good?

1 Like

You could try this

local playerScripts = script:FindFirstAncestorOfClass("PlayerScripts") :: typeof(game:GetService("StarterPlayer").StarterPlayerScripts)
1 Like

Pretty lengthy, but it works. Cool.

1 Like

Roblox already has some smart DataModel typings. Indexing the PlayScripts from the LocalPlayer directly will give you proper results.

local Players = game:GetService("Players")

local LocalPlayer = Players.LocalPlayer
local PlayerScripts = LocalPlayer.PlayerScripts
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.