Help with rename chat command

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? :scream:

    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)
3 Likes

You can’t rename players. It will throw an error.

What you have to do:

  1. Get the character of the player you want to rename.
  2. Find their humanoid.
  3. Set the humanoid’s DisplayName property to whatever you want to change it too.

To save their name when they leave:

  1. Save the display name of their humanoid that you changed earlier.
  2. Then, when they rejoin, set the display name back to the thing you saved.
3 Likes

Hi, I would say it is impossible to change the name when they leave it’s stay.

1 Like

How could I implement something like this with the chat message? I am somewhat confused with this part… :scream:

and is it really not possible to keep the player’s name changed?

1 Like

Not for the chat tag. But for the DisplayName, ues.

use DataStoreService
like this:

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 think this should work

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)

got an error here :scream: I’m checking but I’m not sure what’s wrong

image

can u send a screenshot of the whole script pls?

is a sintaxis error:

image

i wll try to fix the code, brb

1 Like

Thanks you a lot brother :frowning:

in the last CharacterAdded "end " have an “)”

1 Like

Oh this works without errors! but the player name does not change :frowning:

oh, i dont know how to fix that i’m sorry :confused:

1 Like

It’s okay, no problem, thank you very much! :smiley: thank you very much for all your effort :smiley:

1 Like

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

Oh i see :cold_sweat: How could I change that msg2 for the text that the player writes? :scream:

Oh yes I understand the problem, give me a bit of time ill get back to you

1 Like