Hello, I need to do a chat command that renders the players, I have this code, is it well done?
And how can I do so that when the player leaves the game and returns they still having this new name?
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
Player.Chatted:Connect(function(message)
-- Rename Player
if Player.TeamColor == BrickColor.new("Lapis") then
for i,Player2 in pairs(Players:GetPlayers()) do
local msg2 = ""
if message == '.Rename ' .. Player2.Name .. msg2 then
print("The player name is ..." .. Player2.Name)
Player2.Name = msg2
end
end
end
end)
end)
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local NewName = DataStoreService:GetDataStore("NewName")
Players.PlayerAdded:Connect(function(Player)
local NewName2 = Instance.new("StringValue")
NewName2.Name = "PlayerNewName"
NewName2.Parent = Player
NewName2.Value = NewName:GetAsync(Player.UserId) or ""
NewName:SetAsync(Player.UserId, NewName2.Value)
Player.CharacterAdded:Connect(function(Character)
if NewName2.Value ~= "" then
Character:WaitForChild("Humanoid").DisplayName = NewName2.Value
end
end
Player.Chatted:Connect(function(message)
-- Rename Player
if Player.TeamColor == BrickColor.new("Lapis") then
for i,Player2 in pairs(Players:GetPlayers()) do
local msg2 = ""
if message == '.rename ' .. Player2.Name .. msg2 then
print("The player name is ..." .. Player2.Character.Humanoid.DisplayName)
Player2.Character.Humanoid.DisplayName = msg2
end
end
end
end)
end)
Players.PlayerRemoving:Connect(function(Player)
Player:SetAsync(Player.UserId, Player.NewName.Value)
end
I can’t really help with the saving part Im not to good with datastores… but here is how you can change their name:
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
Player.Chatted:Connect(function(message)
-- Rename Player
if Player.TeamColor == BrickColor.new("Lapis") then
for i,Player2 in pairs(Players:GetPlayers()) do
local msg2 = ""
if message == '.Rename ' .. Player2.Name .. msg2 then
print("The player name is ..." .. Player2.Name)
local player2Char = Player2.Character
local player2hum = player2Char:WaitForChild("Humanoid")
player2hum.DisplayName = msg2
end
end
end
end)
end)
In the past I normally have changed the model name of the character. The model name of a player’s character in the past is usually what results in the username above their head.
I haven’t been keeping up much with DisplayNames, but last I heard I thought they had gotten disabled, at least, temporarily.
Oh I think I know what the problem is. You wrote in the that if the player types .rename then the name and then the new name needed, it will work. This should’ve worked, but look, you wrote '.rename '…Player2.Name…msg2. This is with NO spaces. Roblox cannot tell if it starts a new word or not. To fix this just at a …" " in the middle. This is how the new script should look like:
if message == '.rename ' .. Player2.Name.."".. msg2 then