Custom Nametag Saving When Respawning

I have a custom overhead nametag for players in my game, and it works just fine including when resetting happens. It’s able to remake the nametag as if the user is just newly joining the game.

However, what I now want it to do is take the old nametag the user had before, including any scripts, TextLabels, etc that may have been added or removed after the player joined, and I want to have all that come back after they respawn exactly how it was before — whether the changes align with the original nametag when they first joined or not. This probably has an extremely simple solution I overlooked, but I’m stumped. Any help would be appreciated!

Code:

game:GetService("Players").PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function()
		local groupRank = plr:GetRoleInGroup(123456789)
		local otg = script.Rank:Clone()
		otg.Parent = game.Workspace:WaitForChild(plr.Name).Head
		otg.Frame.Name1.Text = plr.Name
		otg.Frame.Rank.Text = groupRank
		otg.Frame.Rank.TextColor = plr.TeamColor
	end)
end)
3 Likes

So essentially, you want a player to retain the exact same nametag they had upon respawning as what they had right before they respawned?

If so, a solution to that could be to store a copy of the nametag right before they respawn, then give it back to them upon respawning:

Example

local Players = game:GetService("Players")


local function createDefaultNametag(player, Head)
    local groupRank = player:GetRoleInGroup(123456789)

    local otg = script.Rank:Clone()
    local Frame = otg.Frame
    local Name1 = Frame.Name1
    local Rank = Frame.Rank

    Name1.Text = player.Name
    Rank.Text = groupRank
    Rank.TextColor = player.TeamColor
    otg.Parent = Head
end


local function onPlayerJoin(player)
    local updatedNametag
    local firstTimeSpawning = true

    player.CharacterAdded:Connect(function(Character)

        local Head = Character:WaitForChild("Head")

        if firstTimeSpawning == true then -- If it's the player's first time spawning into the server...
            firstTimeSpawning = false
            createDefaultNametag(player, Head) -- Create the default nametag for the player
        else

            if updatedNametag == nil then 
                createDefaultNametag(player, Head)
            else
                updatedNametag.Parent = Head
            end
        end
    end)

    player.CharacterRemoving:Connect(function(oldCharacter)
        local Head = oldCharacter:WaitForChild("Head")
        local existingNametag = Head:WaitForChild("Rank")

        if existingNametag then
            updatedNametag = existingNametag:Clone()
        end
    end)
end


Players.PlayerAdded:Connect(onPlayerJoin)

for _, player in Players:GetPlayers() do
    task.spawn(onPlayerJoin, player)
end

I’m pretty sure it’ll work, but if it doesn’t, let me know and I’ll see if I can fix it. And if you’d like me to clarify how any of the code works, feel free to ask!

2 Likes

Thanks for the help, this works as expected! I do have a question though about something I think occurred due to this new script, here’s a picture of the output:

Any idea what this means?

1 Like

Supposedly that warning could be stemming from one of the current Roblox Studio Beta features.

Not sure how to prevent that from happening or how much of an effect it may have on the game, but if it really is related to that beta feature, I imagine it would be fixed before they plan to fully release it in the first half of this year.

If it seems like it breaks anything, I’ll see if I could modify the code I provided to get around that but I think it’ll work fine

2 Likes

Gotcha. No issues are arising from it, I was just wondering what it meant. Thanks for the help!

2 Likes

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