How do I call the function for this script (Local Script that fires when a player joins)

local GUI = game:GetService("StarterGui")
local GUI2 = GUI.ScreenGui
local textLabelScript = GUI.ScreenGui.Frame.TextLabel.LocalScript2
local player = game:GetService("Players")

game.Players.PlayerAdded:Connect(function(plr) 
	print("Player Joined")
	task.wait(1)
	GUI2.Enabled = true
	player.PlayerAdded.GUI.Enabled = true
end)

For context, I created a proper function that runs once the player joins the game. However, the output doesn’t print anything and I have tried to call the function by adding another line of code however it returns an error instead. Is there any way to call the function for game.Players.PlayerAdded:Connect(function(plr) ? I do not know how to call the function and I have tried but as I’ve said, it returns an error and not calling the function would obviously do nothing. If anybody is here, please help! Thanks!

5 Likes

player.PlayerAdded.GUI.Enabled = true
This line will throw an error. player.PlayerAdded is not a real thing. Additionally, you need to edit the player’s personal UI, which is their PlayerGui. Changing things in StarterGui will only change future instances, for everyone.

You need to use the plr parameter that is in your anonymous function. It points to the player that joined (in other words, the answer to the question “which player fired PlayerAdded?”)

local PlayerService = game:GetService("Players")

PlayerService.PlayerAdded:Connect(function(plr) 
    print("Player joined")
    task.wait(1)
    -- Modify items in player's local UI instance
    local thisPlayerGui = plr:WaitForChild("PlayerGui") -- ensure the PlayerGui has loaded
    ...
end)
3 Likes

Where should I put the ScreenGui instead of StarterGui if it affects everyone? It’s in StarterGui as of right now. I also did as what you said but since I don’t have enough clarification to figure out how I should approach and fix the problem, I did this, which also doesn’t work.

local GUI = game:GetService("PlayerGui")
local GUI2 = GUI.ScreenGui
local textLabelScript = GUI.ScreenGui.Frame.TextLabel.LocalScript2
local PlayerService = game:GetService("Players")

PlayerService.PlayerAdded:Connect(function(plr) 
	print("Player joined")
	task.wait(1)
	-- Modify items in player's local UI instance
	local thisPlayerGui = plr:WaitForChild("PlayerGui") -- ensure the PlayerGui has loaded
	GUI2.Enabled = true
	PlayerService.PlayerAdded.GUI.Enabled = true
end)

Could you please give me more clarification of what I should do to make it PlayerGui instead? Also, if you want to know the order, it’s this.
d98bf3f11f6e34104f644a64d78054c4

For context, ScreenGui is NOT enabled. Since I didn’t get enough clarification, the script is located in StarterPlayerScripts. What do I need to change?

2 Likes

Each PlayerGui instance is unique to that player. You can’t just wait for the PlayerGui in a ServerScript. If you modify something in StarterGui at run-time, every player that joins after that change will recieve the updated UI information in StarterGui.

Also, why is your ScreenGui in StarterPlayerScripts? Interesting place to put it.

Here’s an example of how you would write a create and write a text box from the server with the text “Hello world!” in it that gets created at run-time for every player who joins.

game.Players.PlayerAdded:Connect(function(PlayerAdded)
    local PlayerAddedGUI = Player:WaitForChild("PlayerGui") -- get the UI of PlayerAdded
    -- Now that you are in this specific player's UI, add the screen UI and text box
    local ThisScreenGUI = Instance.new("ScreenGui", PlayerAddedGUI)
    local ThisTextBox = Instance.new("TextLabel", ThisScreenGUI)
    ThisTextBox.Text = "Hello world!"
end)

What exactly is the issue about the code? I don’t understand what problem is trying to be solved. That’s the issue. I tried your code and I know how it went out; didn’t work. If it has to do with a total rewrite of the code, tell me what needs replacing, I cannot get the issue since you seem to not be clarifying enough or trying to solve the issue in terms of telling me what is the issue with the code. I do not know how what I am doing, since I’m not very good at scripting and I’m just trying to get better overtime. It’s almost as if when you tell me an explanation, you expect me to understand everything in a sitting. As I’ve said before, could you please give me more clarification of what I should do to make it PlayerGui instead, or tell me how to rewrite the code? It’s frustrating when I don’t have a good enough clarifying explanation that connects to the main problem of the code, what’s wrong and what do I need to approach differently to solve the problem. An example may do it like you gave me, but I tried it in studio and it didn’t show anything.

1 Like

I am not going to write your code for you - in fact, that’s explicity disallowed according to the rules of #help-and-feedback:scripting-support. In order to truly understand your code, you do need to understand the elements in play and what does what. Simply copy/pasting solutions will not educate you. If you are just looking for me to give you the explicit solution, I will not - you can either attempt to solve the issue yourself or wait for someone else to give you it. I am happy to guide you on the right track, but I do not believe simply giving solutions is helpful to anyone unless it’s a strange/irritating issue that doesn’t necessarily warrant an explanation.

(I am assuming you are trying to do this with a ServerScript. If not, please clarify, as it isn’t obvious from the code you’ve provided.)

I will attempt to be more clear, however. Your initial script is attempting to change the UI elements in StarterGui. Big no no, because this is change is not going to be reflected to any players. If you want to change the UI of a specific player, at run-time (while the game is running), you need to instead change the elements of the PlayerGui of that specific player. In your case, I assume you are trying to enable some LocalScripts inside of a UI once a player spawns. What you currently have does not work.

This is because the PlayerGui is what contains the UI elements each player sees, not the StarterGui. Every time a player joins a server, the elements in StarterGui are copied over (replicated) to that specific player’s PlayerGui. This happens every time the player dies and respawns, as the contents of each individual player’s PlayerGui are emptied once that player dies. (there is an exception to this rule that isn’t applicable here)

To fix this issue, you need to enable these elements in the player’s PlayerGui, which I hinted to in my first response. Your first attempted solution has this erroneous line of code:

local GUI = game:GetService("PlayerGui")

This will not work, because PlayerGui is not a service nor is it something that can be asked for from a ServerScript without first getting the player it’s attached to. Thus, your GUI2 variables and textLabelScript are also erroneous.

local PlayerService = game:GetService("Players")
PlayerService.PlayerAdded:Connect(function(plr) 
	print("Player joined")
	task.wait(1)
	-- Modify items in player's local UI instance
	local thisPlayerGui = plr:WaitForChild("PlayerGui") -- ensure the PlayerGui has loaded
	...
end)

Refer back to the second half of the code in my response. All you have to do is enable these elements in this player’s PlayerGui. To do that, you can surf through the PlayerGui. It will contain exactly what your StarterGui contains. (for example, thisPlayerGui.ScreenGui.TextLabel... bla bla bla)

Issue: your UI is not in StarterGui. Again, I don’t know why this is the case, but if it’s for a reason you are going to have to deal with the additional hassle of manually replicating the UI to the player’s PlayerGui. I assume this is not what you want.

To solve your issue, you need to do the following:

  • Place your UI in StarterGui
  • Use the ServerScript that is listening for player connections to change the UI that was replicated to that player’s PlayerGui

Now, since the UI resets on a player’s death, you’ll also have to listen for when their character respawns as well. Since that’s not within the scope of this issue, I am not going to elaborate on it, but it’s a relatively easy exercise that can be left to the reader.

I forgot to state this, but the scripts I’m working with are LocalScripts, not ServerScripts. Sorry for the inconvenience.

I’ve made a crucial error to explaining what script I’m trying to accomplish.

For the script I’ve been trying to make, it’s as how this goes: A ScreenGui will pop up after the LocalScript (This script that I’m trying to get debugged) enable ScreenGui and the 2nd LocalScript (LocalScript2), which will activate the 2nd script, causing the game to set the TextLabel to talk, etc.

So where exactly should I put the ScreenGui script, etc in? I know that StarterPlayer and it’s subcategories won’t do it. I think workspace is possibly my best bet but I have doubts about it. I would have to go through the hassle of creating another script in the workspace to create a Gui that is only visible for each of the players. I’m not entirely sure on what I should try and do to solve the problem. The most I can think of is removing the Guis, keeping one local script in StarterGui and then rewriting it to create a PlayerGui instead, which I don’t know if thats the right track or not. If that is what you are trying to tell me, I might have gotten your point. All I need is what do I need to put inside StarterGui, whether it’s just a local script running all the commands, creating a PlayerGui (see paragraph 2 for what I’m trying to achieve), etc. Since you seem to be against the fact that I am running ScreenGUi → Frame → TextLabel, etc.
e59f12015503935a258e6fa5b4f66920c48f55e8
(Except for the fact it’s in StarterGui now, not StarterPlayerScripts.)

Please if this is right; Making a localscript that commands everything from creating a playergui to making the PlayerGui text change for the player.

You seem to be missing what @Negativize is pointing out.

The best common practice would be to modify the gui from Player.PlayerGui. This does NOT replicate to other players because you are doing this through a local script.

Not too sure where you’re getting the idea that you’ll have to rewrite everything or move your scripts to Workspace. You just need to correct your paths for the Gui.

Using your advice, this is what I tried to do.

local PlayerService = game:GetService("Players")

PlayerService.PlayerAdded:Connect(function(plr) 
	print("Player joined")
	task.wait(1)
	-- Modify items in player's local UI instance
	local thisPlayerGui = plr:WaitForChild("PlayerGui") -- ensure the PlayerGui has loaded
	local screenGui = thisPlayerGui.ScreenGui
	local script2 = thisPlayerGui.ScreenGui.Frame.TextLabel.LocalScript2
	screenGui.Enabled = true
	script2.Enabled = true
end)

Through your explanation, I managed to understand better since it was more concise. However, I still didn’t get exactly what I wanted, that being the ScreenGui and LocalScript2 being enabled upon playerjoin/reset. I declared what I needed but it looks like when I run the game and check the Player.PlayerGui into ScreenGui and LocalScript2, the property of being Enabled isn’t checked off. I’m not sure why this is happening but I think I get what form of explanation I’m being told. Is there something that I am forgetting to declare to track PlayerGui or anything for ScreenGui, etc?

If this is a local script you don’t need to use the PlayerAdded event, you can just reference Players.LocalPlayer.

Tried it, got an error at PlayerService.LocalPlayer:Connect. Said that Connect is not a valid member of Player "Players.borenreef" . I’m not sure what I should write to fix the error, since I’m not entirely familiar with Luau.

Why are you trying to use Connect on LocalPlayer?

You literally just reference the player as Players.LocalPlayer, this returns the player instance.

Tried it, instead it runs a error of this: Players.borenreef.PlayerGui.ScreenGui.Frame.TextLabel.LocalScript:3: attempt to call a Instance value - Client - LocalScript:3

Heres the product:

PlayerService.LocalPlayer(function(plr) 
	print("Player joined")
	task.wait(1)
	-- Modify items in player's local UI instance
	local thisPlayerGui = plr:WaitForChild("PlayerGui") -- ensure the PlayerGui has loaded
	local screenGui = thisPlayerGui.ScreenGui
	local script2 = thisPlayerGui.ScreenGui.Frame.TextLabel.LocalScript2
	screenGui.Enabled = true
	script2.Enabled = true
end)

There has to be something I’m missing for PlayerService.LocalPlayer(function(plr) at PlayerService.LocalPlayer and the 3rd term.

You’re attempting to create an event with an object. You don’t need to wrap your code in an event.

I’m on mobile so I’m not able to format code properly but you should just be doing this:

local Player = PlayersService.LocalPlayer
local PlayerGui = Player.PlayerGui

Where should I put the code? I put local Player = PlayersService.LocalPlayer for declaring variables and local PlayerGui = Player.PlayerGui inside the function, which just resulted in another error. What am I doing wrong with the code?

local Player = PlayersService.LocalPlayer

PlayerService.LocalPlayer(function(plr) 
        local PlayerGui = Player.PlayerGui
	print("Player joined")
	task.wait(1)
	-- Modify items in player's local UI instance
	local thisPlayerGui = plr:WaitForChild("PlayerGui") -- ensure the PlayerGui has loaded
	local screenGui = thisPlayerGui.ScreenGui
	local script2 = thisPlayerGui.ScreenGui.Frame.TextLabel.LocalScript2
	screenGui.Enabled = true
	script2.Enabled = true
end)

I need a better explanation of what I’m doing wrong. I start thinking its PlayerService.LocalPlayer for the function line because of the errors, when it possibly can’t.

I’m still 100% lost as to what you’re doing. LocalPlayer is not a function. It’s a static reference to the local player. You’re trying to use it as an event and that’s not possible or necessary.

Your code should literally just be:

local Player = PlayersService.LocalPlayer
local PlayerGui = Player.PlayerGui
print("Player joined")
task.wait(1)
-- Modify items in player's local UI instance
local thisPlayerGui = plr:WaitForChild("PlayerGui") -- ensure the PlayerGui has loaded
local screenGui = thisPlayerGui.ScreenGui
local script2 = thisPlayerGui.ScreenGui.Frame.TextLabel.LocalScript2
	screenGui.Enabled = true
	script2.Enabled = true
end)

You don’t need to even wait for PlayerAdded, local scripts don’t run until the player joins.

It worked, though I had to modify the code to this.

local PlayerService = game:GetService("Players")

	local Player = PlayerService.LocalPlayer
	local PlayerGui = Player.PlayerGui
	print("Player joined")
	task.wait(1)
	-- Modify items in player's local UI instance
	local thisPlayerGui = Player:WaitForChild("PlayerGui") -- ensure the PlayerGui has loaded
	local screenGui = thisPlayerGui.ScreenGui
	local script2 = thisPlayerGui.ScreenGui.Frame.TextLabel.LocalScript2
	screenGui.Enabled = true
	script2.Enabled = true
	script:Destroy()

I feel completely dumbfounded when I am given the solution instead. All it took was removing the function for the LocalScript.