Log in to vote

How do you insert the players username in a custom npc dialogue gui?

Asked by 10 days ago

I'm trying to make a custom npc dialogue thing and I want to put the players username in the text but I can't figure it out for some reason. I have this so far which works

local TextLabel = script.Parent TextLabel.Text = game.Players.LocalPlayer.Name

But idk how to put it in the text for example,

script.Parent.text = ("Hi (Says their username) how are you doing)

Ik there's probably something similar to this post but it doesn't have what im looking for.

2 answers

Log in to vote
Answered by 10 days ago

Since you want to get the player's username in the text, you want to use concatenation.

Concatenation is grouping or combining two strings together.

--Get Player
local Player = game.Players.LocalPlayer

--Reference Text Label
local TextLabel = script.Parent.TextLabel

--To Concatenate the Player's Name to the Text Label you would want to do this:
TextLabel.Text = "Hello there "..Player.Name..", how are you doing?"

Here is another example on concatenation.

--Get Player
local Player = game.Players.LocalPlayer

--Reference Text Label
local TextLabel = script.Parent.TextLabel

--Lets Split It Up To Multiple Strings to make it easier to understand)
--Rather than do what I did in the top, lets split the entire sentence into multiple strings.
local string1 = "Hello there"
local string2 = ", how are you doing"
--To Concatenate the Strings together, in Lua, you use ".."
TextLabel.Text = string1..Player.Name..string2
--This will result in the same output as the example previous example.
Log in to vote
Answered by
Speedmask 540 Moderation Voter
10 days ago
Edited 10 days ago

arson has a good answer. if you want to get more complicated eventually, it can be easier on you to use string.format(). here's an example of what you would want to do using formatting instead.

local player = game:GetService("Players").LocalPlayer
local greeting = "whaddup %s, how's it going?"

print(string.format(greeting, player.Name))

-- these also do the same thing
print(("whaddup %s, how's it going?"):format(player.Name))

%s gets replaced with the player's name.

if you read the link I gave, you will find that you can do this with multiple strings, numbers and more, so it is probably good if you are writing a lot of dialogue. that said, concatenation is also fine for simpler phrases. or like, if you don't care.

Answer this question