Text label saving issues

I’ve made a name changing system for my game.
The problem is that every time the player resets their name turns into their username rather then
their custom name.

Before

On death

I’ve tried EVERYTHING and I have been working for weeks just to get this fixed
Even resorting to AI just to get this to work.
I’ve just started scripting a month ago and nothing I’ve done to fix this works.
Scripts and stuff:

local textBox = script.Parent
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local UpdateOverheadName = ReplicatedStorage:FindFirstChild("UpdateOverheadName")

-- Always initialize lastOverheadName to the current TextBox text
local lastOverheadName = textBox.Text

-- When the player finishes typing, update the overhead name and store it
textBox.FocusLost:Connect(function(enterPressed)
	if enterPressed and UpdateOverheadName then
		local player = Players.LocalPlayer
		local newName = textBox.Text
		lastOverheadName = newName
		-- Fire to server to update for all players
		UpdateOverheadName:FireServer(newName)
		-- Immediately update local overhead name GUI
		local character = player.Character
		if character then
			local head = character:FindFirstChild("Head")
			if head then
				local overheadGui = head:FindFirstChild("OverheadNameGUI")
				if overheadGui then
					if overheadGui:FindFirstChild("TextLabel") then
						overheadGui.TextLabel.Text = newName
					end
					if overheadGui:FindFirstChild("TextLabels") then
						overheadGui.TextLabels.Text = newName
					end
				end
			end
		end
	end
end)

-- Function to set overhead name GUI for a player
local function setOverheadName(player, newName)
	if player and player.Character then
		local head = player.Character:FindFirstChild("Head")
		if head then
			local overheadGui = head:FindFirstChild("OverheadNameGUI")
			if overheadGui then
				if overheadGui:FindFirstChild("TextLabel") then
					overheadGui.TextLabel.Text = newName
				end
				if overheadGui:FindFirstChild("TextLabels") then
					overheadGui.TextLabels.Text = newName
				end
			end
		end
	end
end

-- Listen for overhead name updates from server
UpdateOverheadName.OnClientEvent:Connect(function(targetUserId, newName)
	local player = Players:GetPlayerByUserId(targetUserId)
	if player then
		if player == Players.LocalPlayer then
			lastOverheadName = newName
		end
		setOverheadName(player, newName)
	end
end)

-- Ensure overhead name persists after respawn
local localPlayer = Players.LocalPlayer
local function onCharacterAdded(character)
	-- Wait for head to exist
	local head = character:WaitForChild("Head", 5)
	if head then
		-- Wait for OverheadNameGUI to exist, up to 5 seconds
		local overheadGui
		local startTime = os.clock()
		repeat
			overheadGui = head:FindFirstChild("OverheadNameGUI")
			if not overheadGui then
				task.wait(0.1)
			end
		until overheadGui or (os.clock() - startTime) > 5

		if overheadGui then
			-- Always set to lastOverheadName if available, never revert to player name
			if lastOverheadName and lastOverheadName ~= "" then
				setOverheadName(localPlayer, lastOverheadName)
			end
		end
	end
end

if localPlayer.Character then
	onCharacterAdded(localPlayer.Character)
end
localPlayer.CharacterAdded:Connect(onCharacterAdded)


local parter = script.Parent
local hum = parter.Humanoid

parter.Humanoid.DisplayDistanceType = "None"
parter.Humanoid.HealthDisplayType = "AlwaysOff"

local gui = script.OverheadNameGUI:Clone()
gui.TextLabel.Text = parter.Name
gui.TextLabels.Text = parter.Name

gui.Parent = parter.Head

local hpbar = script.OverheadHealthGUI:Clone()
hpbar.Parent = parter.Head

hum:GetPropertyChangedSignal("Health"):Connect(function()
	local hp = hum.Health/hum.MaxHealth
	local bar = hpbar.HealthBarContainer.Bar
	bar:TweenSize(UDim2.new(hp,0,1,0), Enum.EasingDirection.In, Enum.EasingStyle.Linear, .3)
end)

-- Change character name when FocusLost
if script.Parent:FindFirstChild("FocusLost") then
	script.Parent.FocusLost:Connect(function(enter)
		if enter then
			local player = game.Players.LocalPlayer
			local character = player.Character or player.CharacterAdded:Wait()
			character.Name = script.Parent.Text
			-- Update overhead GUI text as well
			local head = character:FindFirstChild("Head")
			if head then
				local overheadGui = head:FindFirstChild("OverheadNameGUI")
				if overheadGui then
					if overheadGui:FindFirstChild("TextLabel") then
						overheadGui.TextLabel.Text = script.Parent.Text
					end
					if overheadGui:FindFirstChild("TextLabels") then
						overheadGui.TextLabels.Text = script.Parent.Text
					end
				end
			end
		end
	end)
end


1 Like

I have been procrastinating and halting development of my game just because of this
Also here is another script I forgot to add to the post

local textBox = script.Parent
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local UpdateOverheadName = ReplicatedStorage:FindFirstChild("UpdateOverheadName")

textBox.FocusLost:Connect(function(enterPressed)
	if enterPressed and UpdateOverheadName then
		local player = Players.LocalPlayer
		local newName = textBox.Text
		-- Fire to server to update for all players
		UpdateOverheadName:FireServer(newName)
	end
end)

-- Listen for overhead name updates from server
UpdateOverheadName.OnClientEvent:Connect(function(targetUserId, newName)
	local player = Players:GetPlayerByUserId(targetUserId)
	if player and player.Character then
		local head = player.Character:FindFirstChild("Head")
		if head then
			local overheadGui = head:FindFirstChild("OverheadNameGUI")
			if overheadGui then
				if overheadGui:FindFirstChild("TextLabel") then
					overheadGui.TextLabel.Text = newName
				end
				if overheadGui:FindFirstChild("TextLabels") then
					overheadGui.TextLabels.Text = newName
				end
			end
		end
	end
end)```
1 Like

On the screen gui is the reset on spawn off or on? (I think this resets the text).

2 Likes

I have a question:

Why do you need to replicate for every client the new name TextLabel of a certain player? If you only update the server, it should be replicated to the client.

2 Likes

It actually worked Im so stupid
tsym

lol it’s good. Did you actually spend a month on this?

More like a 1-2 weeks. I was over exaggerating

1 Like

Im going to sleep. Thanks for the help

1 Like

Your welcome. It happens to the best of us..

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.