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:
(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?
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.
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.