GUI To Change Player Name

Hey, so I’m trying to make this button that when you press it, it changes your overhead name to “Trainee”, but everything I’ve tried so far doesn’t work.

Here is my code:


local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character
local Continue = script.Parent

Continue.MouseButton1Click:Connect(function(player)
player.Character.Rank.Frame.TextLabel.Text = "Trainee"

end)

The error I’m getting is:

I would appreciate it if anyone could help me.

Cheers :heart:

You had the variable “player” used two times. I think that might’ve confused the game.
Just remove the player from the function and try again.

You don’t need to put parameter to the function of MouseButton1Click if it is already a local script. leave it blank and use the initialized context of player you’ve done.

I removed it from the function and it’s still giving the same error.

I just noticed you are using a LocalScript to display the rank, which will result in only yourself being able to see your rank. Is that intentional?

There might be the slight chance that the Character Model might not fully load in time when the Player first spawns, hence why it results as an error attempt to index nil with 'Character'

Use the CharacterAdded:Wait() as well, which will yield whenever the Character starts to first load into the game:

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local Rank = character:WaitForChild("Rank") --Highly suggest waiting for the Child before changing its properties
local Continue = script.Parent

Continue.MouseButton1Down:Connect(function() --Cause it to MouseButton1Down cause it works strange if you're using Click
    Rank.Frame.TextLabel.Text = "Trainee"
end)

I’ve tried this and it only works locally, if I put it onto a script it gives the error. It changes the name tag locally but not globally.

While localizing, you used character with a lowercase but in the function, you capitalized it.

probaly this might work (make it a normal script)


local Continue = script.Parent

Continue.MouseButton1Down:Connect(function(player)
local char = player.Character or player.CharacterAdded:Wait()
local Rank = char:WaitForChild("Rank")
    Rank.Frame.TextLabel.Text = "Trainee"
end)

nvm change it back to local script and change it to this:

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

Continue.MouseButton1Down:Connect(function(player)
	local Rank = char:WaitForChild("Rank")
	Rank.Frame.TextLabel.Text = "Trainee"
end)

If you want to change it “globally” you’ll have to use a RemoteEvent to be able to properly change the Text from the client to the server, call FireServer() from your local player, and detect those said Events from OnServerEvent:

--Client 
local Players = game:GetService("Players")
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent") --RemoteEvents should usually be in ReplicatedStorage to be able to be detected through both sides

Continue.MouseButton1Down:Connect(function()
    Event:FireServer()
end)
--Server Script, preferably inside ServerScriptService
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent") --RemoteEvents should usually be in ReplicatedStorage to be able to be detected through both sides

Event.OnServerEvent:Connect(function(Plr) --The Plr is automatically passed as the 1st argument
    local Char = Plr.Character

    if Char and Char:FindFirstChild("Rank") then
        Char.Rank.Frame.TextLabel.Text = "Trainee"
    end
end)
1 Like