Making a Table With Variables & Strings In It?

Im trying to create a system that welcomes new players to a game I’m working on, a simple chat message.

I wanna have a table with a bunch of welcome messages that include the players name, similarly to how discord does this:
image
(White rectangles are user names)

Im probably forgetting a certain thing exists, but how would I have areas of the message that include the players name, without using if statements?

like this

string.format("%s welcome to my game!", plr_name)
1 Like

If you’re referring to inserting a name into a pre-set string, use formatting.

Example:

local String = "%s has joined the game!"
String:format(Name)
1 Like

Thank you for taking the time to reply, I can only have one answer. : (

I very much appreciate it.

1 Like

You don’t even need to use string.format() for this, player.Name.." has joined! would suffice.

Not looking for the solution mark, just letting you know.

1 Like

I tried this, the issue is that I was trying to make a table of presets that included the players name, & I couldn’t have a variable combined with a string in a table, so I would’ve had to use if statements.

local players = game:GetService("Players")

local presets = {" has joined!", " has left!"}

players.PlayerAdded:Connect(function(player)
	print(player.Name..presets[1])
end)

players.PlayerRemoving:Connect(function(player)
	print(player.Name..presets[2])
end)

string.format() would be the way to reserve a string for the player’s name though.



image

Stirng.format is a lot cleaner and easier to read. Also string.format allows for translation, while concatenation does not. String.format is the optimal solution imo

Depends, string.format() has a greater overhead than simple string concatenation. As shown in the screenshots above string value reserving is required so string.format() is the only viable option.