Hiya! After some late-night coding, I more or less finished my RP Nametag feature. It allows you to set a (filtered) name, and change the font and colours from some pre-selected ones.
I still need to code for when the player resets/dies, but I want to get my code reviewed first before I do that.
Keep in mind, I’m VERY new to Roblox/Lua, and this is actually my first real project! Like, ever! So with that said, here goooeees!
Here is a video of the feature in action!
And here is the code of the Server script that handles it all;
-- StringValue for the Name
game.Players.PlayerAdded:Connect(function(player)
local rpName = Instance.new("StringValue", player)
end)
-- When enter is pressed, create a Nametag and change it to the Name
game.ReplicatedStorage.SetRpName.OnServerEvent:Connect(function(player, name)
print(name)
-- Filtering for Public Use
if game:GetService('TextService'):FilterStringAsync(name,player.userId,Enum.TextFilterContext.PublicChat):GetNonChatStringForBroadcastAsync() == name then
local rpName = name
print(rpName)
local head = player.Character.Head
local usertag = game:GetService("ServerStorage"):WaitForChild("UserTag")
local clonedtag = head:FindFirstChild("UserTag") or usertag:Clone()
clonedtag.TextLabel.Text = rpName
clonedtag.Parent = head
else
-- If Text is filtered, the Nametag will not update (tested)
error('Text filtered! Original string: '..name..' filtered string: '..game:GetService('TextService'):FilterStringAsync(name,player.userId,Enum.TextFilterContext.PublicChat):GetNonChatStringForBroadcastAsync())
end
end)
-- Changing the Font of the Nametag
game.ReplicatedStorage.SetFont.OnServerEvent:Connect(function(player, font)
local nameFont = font
local head = player.Character.Head
local clonedtag = head:FindFirstChild("UserTag")
clonedtag.TextLabel.Font = font
end)
-- Changing the colour of the nametag
game.ReplicatedStorage.SetColour.OnServerEvent:Connect(function(player, colour)
local nameColour = colour
local head = player.Character.Head
local clonedtag = head:FindFirstChild("UserTag")
clonedtag.TextLabel.TextColor3 = nameColour
-- To compensate for Colour4 vvv
clonedtag.TextLabel.TextStrokeColor3 = Color3.fromRGB(26,26,26)
end)
-- Changing the colour of the Nametag to Black w/ a White Stroke
-- Better way to do this?
game.ReplicatedStorage.SetColour4.OnServerEvent:Connect(function(player, colour)
local nameColour = colour
local head = player.Character.Head
local clonedtag = head:FindFirstChild("UserTag")
clonedtag.TextLabel.TextColor3 = nameColour
-- Manual stroke colour below vv
clonedtag.TextLabel.TextStrokeColor3 = Color3.fromRGB(243,243,243)
end)
Apart from anywhere I could clean the code up/make it less redundant - is there any better way to do the Fourth Colour option? Basically, unlike the other options, it sets a different TextStrokeColor3 value as well (white, to offset the black), however, I couldn’t really figure out how to send a third value through a RemoteEvent? Or if that’s even possible?
I don’t mind leaving it if it’s not really a concern, I probably won’t have anyone collaborating on the code, if it’s relevant.
Thanks for any pointers!