Is this efficient, or should I avoid this at all costs?

I know other ways to substitute for doing this, yet it seems like a simple way of creating what I need.

Let’s say I have a UI in StarterGui, and I place a script inside of a textlabel. Is it non-efficient to use script.Parent.Parent.Parent… (you get it) to try and get the player?

in local script you can use game.Players.LocalPlayer
in script you can use game.Players.PlayerAdded:Connect(function(Player) or script.Parent... -- like what you said

I know, but is this a bad way of doing it, and should I use remoteEvents instead?

it’s really your choice if what kind of script you want. The important thing is the game runs as what you wanted.

Only local scripts will execute in StarterGui so any of the following ways are valid methods of retrieving the local player.

local player = game.Players.LocalPlayer
local player = game:GetService("Players").LocalPlayer
local player = script:FindFirstAncestorWhichIsA("Player")
local player = script:FindFirstAncestorOfClass("Player")
local player = script:FindFirstAncestorWhichIsA("ScreenGui").Parent.Parent
local player = script:FindFirstAncestorOfClass("ScreenGui").Parent.Parent

The top 2 are always going to be the most efficient.

1 Like

I’m pretty sure scripts also execute by what I’ve seen, but thanks for the info!

The instant that the following conditions are met, a Script’s Lua code is run in a new thread:

  • Disabled property is false
  • The Script object is a descendant of the Workspace or
    ServerScriptService

Server scripts (regular scripts) will not execute if placed inside the StarterGui folder.

That is very interesting. I used scripts in starterGui and everything works fine… I tested it by placing a script in StarterGui with

while wait() do
	print("works...")
end

and it did print, in studio and in Roblox Player.

script.Parent… would only be valid in server scripts if the script is parented to the player/character, if it’s just inside the Workspace/ServerScriptService folder then .Parent will reference those folders (not the player/character).

StarterGui is replicated into the PlayerGui, so I believe it is fine. As for the script not running elsewhere than workspace/SSS, I’m really unsure on that.

That’s because the StarterGui content (including any scripts) is copied into the PlayerGui folder of each player instance each time their character spawns/reloads. It’s ill-advised to use server scripts to handle the the player’s UI as it adds unnecessary strain on the server. The UI need only be relative to the local player and as such should only and always be handled on the client-side through local scripts.

1 Like

Short Answer: It isn’t Time Efficient and you will Burn Out.

Long Answer

I don’t know if there’s a performance difference, but if there was, it would be next to nothing. It’ll get harder to read your script as you continue to add more .Parent's until eventually, you get lost.

It’s generally good practice to start a variable from an explicit holder of related items. Examples would be: ScreenGui, PlayerInstance, Folder. Pretty much the Main Holder of all your instances of X type or for Y purpose.

Eventually you’re going to have to make changes, and changing 50 of this: script.Parent.Parent.Parent.Parent to script.Parent.Parent.Parent.Parent.Parent for the millionth time will not be fun. Find and Replace might add or remove more .Parent's than you wanted and that brings even more problems.

You’ll burn yourself out if you don’t keep thing easy for yourself.

-- As stated by others, things like this may help you quite a lot when it comes to avoiding script.Parent spam
local ScreenGui = script:FindFirstAncestorOfClass("ScreenGui")