Local Script wont change text label, and Server Script only changes it for the Server which players cant see?

I am trying to make simple dialogue, for some reason I can’t change the text in a TextLabel from a Local Script, In a server script this works fine but only the server can see it, if you switch to the players client the text only changes once to “hi”, I am new to scripting and this continues to be a issue since I can’t find a good solution to my problem.

I even tried the code alone without the function with a print at the end, but still doesn’t change the text label but will for some reason still print into the output.

I also checked the TextLabel it’s self which I found nothing wrong with it.

local players = game:GetService("Players")

local ui = game:GetService("StarterGui")
local txt = ui.Dialogue.txt

local re = game:GetService("ReplicatedStorage")

local ev = re.PlayerJoined

local function OnClientEvent(ev)
    
    txt.Text = ("hi")
    
    task.wait(1)
    
    txt.Text = ("who are you?")
    
    task.wait(1)

    txt.Text = (" ")
    
end

players.PlayerAdded:Connect(OnClientEvent)

2 Likes

Hello, how are you doing!

From what can be seen, what you’re setting is the StarterGui’s GUI instead of the player’s GUI. If you want it to activate to the player, there are two things you could do:

1- If you just want to test it server-wise to see if it works, inside the OnClientEvent() function, you can make a variable for the player’s GUI, not the StarterGui.

local playerGui = ev.PlayerGui

With this, you can then use it to change the player’s text as it follows:

local txt = playerGui.Dialogue.txt

Keep in mind this needs to be inside the function, and you would need to remove the previous “txt” and “ui” variables outside to refrain from having any sort of confusion.

2- Now for the actual optimization, you can make so when the player joins, a remote event is fired, where a local script will fire a function through .OnClientEvent:Connect(function()) and change the text of the GUI. The steps would be the same, except this time, to get the GUI, it would be as simple as:

local playerGui = game.Players.LocalPlayer.PlayerGui

Hope this could help!

Edit: I have just noticed your function actually contains the same name as the event. For this, I would recommend changing the name of your function to not cause any sort of confusion later.

3 Likes

Hey there! Thank you for helping me!

I attempted to follow your instructions and edited the script.

The issue now is I get a error, tried playing around with the script to see if I could get it working and resolve the error but I’m out of luck.

Error: Dialogue is not a valid member of PlayerGui "Players.TheAzureGaming.PlayerGui"

image_2024-06-07_185135208


local re = game:GetService("ReplicatedStorage")
local ev = re.PlayerJoined

local plrUI = players.PlayerGui

local text = plrUI.Dialogue.txt

local function OnClientEvent(ev)

	text.Text = ("hi")

	task.wait(1)

	text.Text = ("who are you?")

	task.wait(1)

	text.Text = (" ")

end

players.PlayerAdded:Connect(OnClientEvent) ```
3 Likes

No worries!

From what I can now see, you’ve placed the plrUI variable outside the function. Let me just explain it for you:

The players.PlayerAdded:Connect(OnClientEvent) will activate and return the player who joined as an argument. When the OnClientEvent(ev) function runs, this means ev contains the player instance. It is from the player’s instance where you get their GUI, as following:

local plrUI = ev.PlayerGui

Then, you can get your dialogue’s TextLabel as you wrote:

local text = plrUI.Dialogue.txt

This will only work inside the function because it is from there where you’re getting the player who has joined the game. Writing it outside the function makes the game not understand where to obtain “PlayerGui”, and therefore, returns an error.

Hope this could help!

2 Likes

Thank you! The error has been resolved, but it doesn’t seem the function is running. Did I not call it right?

I also tried testing the function by printing a message in the output but nothing showed.


local re = game:GetService("ReplicatedStorage")

local function OnClientEvent(ev)
	
	local plrUI = players.LocalPlayer.PlayerGui
	local text = plrUI.Dialogue.txt

	text.Text = ("hi")

	task.wait(1)

	text.Text = ("who are you?")

	task.wait(1)

	text.Text = (" ")

end

players.PlayerAdded:Connect(OnClientEvent) ```
2 Likes

No problem!

Also, you think you could please maybe show where is the script located? Is it a server script or a local script still? Could you show output? I’m not sure of why it isn’t printing, but maybe if we break this down, we can get your issue to be fixed.

2 Likes

Ofcourse! it is a local script located in StarterPlayerScripts. Would it be better if I were to change it to a Server Script?
image

2 Likes

My apologies here is the output, it’s completely blank

1 Like

Oh… I thought it was previously a server script, that’s why I gave you the idea that it could’ve used a remote event to be fired when the player joins.

Let me explain it for you:

The .PlayerAdded:Connect() fires everytime a player joins the game. I now understand what you’re trying to do, and that is not needed. The LocalScript should already run when the player joins since it’s on StarterPlayerScripts.

What you could do is simply remove the entire .PlayerAdded as it will not be needed for now on. If you want the script to execute after the player has loaded into the game, I’d recommend waiting for when the player’s character loads, right before the function, by using a task.wait() inside a repeat.

repeat task.wait() until players.LocalPlayer.CharacterAdded:Wait()

What .CharacterAdded does is fire whenever the player’s character is added, and :Wait() simply waits for when the event fires. Since we’re not really attribuing it to any variable, it will not repeat itself later and prevents any confusion or glitches afterwards.

But since the only thing you’re doing is actually just change the text, you don’t actually need to place it inside a function and make it run. Instead, you can just run it straightforward, and since you’re in a local script, you will not need to use the ev variable as I mentioned, since players.LocalPlayer will already get your own player.

I’m not sure if you’re doing this just to test if your dialogue can change its text, so what I would actually recommend is placing it inside a task.spawn() so everything that comes after these lines of code will still run even while the text is being changed.

local re = game:GetService("ReplicatedStorage")
local player = players.LocalPlayer
local plrUI = player.PlayerGui
local text = plrUI.Dialogue.txt

repeat task.wait() until player.CharacterAdded:Wait()

local function OnClientEvent()
	text.Text = ("hi")

	task.wait(1)

	text.Text = ("who are you?")

	task.wait(1)

	text.Text = (" ")

end

task.spawn(onClientEvent)
1 Like

I now get a error, when moving the Local Script to StarterGui from StarterPlayerScripts the error goes away. But the TextLabel still doesn’t change.

1 Like

Could you try getting it to print something before the first time the Text change, moving it back to StarterPlayerScripts and making the task.wait() longer for about, let’s say, 5 seconds? I’m suspecting something, but let’s break it down first.

1 Like

I changed the task.wait() to 5 seconds, I am still getting a error on line 6.

Error: Dialogue is not a valid member of PlayerGui "Players.TheAzureGaming.PlayerGui" - Client - dia 1:6


local re = game:GetService("ReplicatedStorage")
local player = players.LocalPlayer
local plrUI = player.PlayerGui
local text = plrUI.Dialogue.txt

repeat task.wait(5) until player.CharacterAdded:Wait()

local function OnClientEvent()
	warn("Warn has printed")
	
	text.Text = ("hi")

	task.wait(1)

	text.Text = ("who are you?")

	task.wait(1)

	text.Text = (" ")

end

task.spawn(OnClientEvent) ```
1 Like

I feel like the script is loading before the UI loads, so try changing plrUI.Dialogue.txt with plrUI:WaitForChild("Dialogue").txt

The error has gone away but the text doesn’t show, I checked the TextLabel itself again to see if maybe it was the issue but it’s perfectly fine.

1 Like

could you show me the dialouge screengui with the text in the explorer?

2 Likes

Yes, ofcourse. I apologies for the wait, had to convert the file to a mp4 since I recorded this with OBS.

1 Like

i think this is because you’re defining the dialouge screengui before doing any wait() so apparently you’re not waiting for the gui to load
try reducing the task.wait(5) to task.wait() and adding a task.wait(2) on top of the script before any variables

1 Like
task.wait(2)
local re = game:GetService("ReplicatedStorage")
local player = players.LocalPlayer
local plrUI = player.PlayerGui
local text = plrUI:WaitForChild("Dialogue").txt

repeat task.wait() until player.CharacterAdded:Wait()

local function OnClientEvent()
	warn("Warn has printed")
	
	text.Text = ("hi")

	task.wait(1)

	text.Text = ("who are you?")

	task.wait(1)

	text.Text = (" ")

end

task.spawn(OnClientEvent) ```
3 Likes

I thought of that too, hence the addition of WaitForChild, but apparently the issue now is that the function will not run at all. Since you are waiting 2 seconds with task.wait(2), it will not reach the player.CharacterAdded:Wait() in time for the script to run it before the character is added, and it will just infinitely wait until the player resets its character.

I’d recommend just removing the repeat task.wait() until player.CharacterAdded:Wait() line and see what happens

plrUI:WaitForChild:("Dialogue"):WaitForChild("txt") without task.wait() on the top could work too but waiting is better