Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 3 years 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
1
Answered by 3 years 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:
--REMEMBER: THE PLAYER'S NAME IS A STRING, BUT NOT THE PLAYER!
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.
Ad
Log in to vote
0
Answered by
Speedmask 661 Moderation Voter
3 years ago
Edited 3 years 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(greeting:format(player.Name))
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