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)
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.
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)
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)