Setting continuing to work even on new players/players who reset

I have a settings GUI that when you enable a button it hides all player’s nametags (custom nametags) and when you disable it then it brings them back, but how can I make this so that even if a player resets (including themselves) or a new player joins, their setting will still apply?

code:

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer

local disabled = script.Parent:WaitForChild("Disabled")
local enabled = script.Parent:WaitForChild("Enabled")
disabled.Enabled = true
enabled.Enabled = false

script.Parent.MouseButton1Click:Connect(function()
    if disabled.Enabled == true and enabled.Enabled == false then -- setting is disabled, client is trying to enable
        for i, plr in pairs(Players:GetChildren()) do
            local head = plr.Character:WaitForChild("Head")
            head:FindFirstChild("Rank").Enabled = false
        end

        enabled.Enabled = true 
        disabled.Enabled = false
        
        return
    end

    if enabled.Enabled == true and disabled.Enabled == false then -- setting is enabled, client is trying to disable
        for i, plr in pairs(Players:GetChildren()) do
            local head = plr.Character:WaitForChild("Head")
            head:FindFirstChild("Rank").Enabled = true
        end
        
        enabled.Enabled = false 
        disabled.Enabled = true
        
        return
    end
end)

To achieve that, the sections of code that update the Enabled property for nametag (BillboardGui?) should be included in an independent function so that it can be activated in multiple ways (e.g. from the PlayerAdded and CharacterAdded events) in addition to whenever the player updates the setting.

I also think that this aspect of the script could be simplified as to avoid the confusion from having separate variables for enabled and disabled; it seems more intuitive to have one or the other (e.g. because enabled == false would be the same as disabled == true, so it doesn’t seem necessary to have both).


Here’s an example revision (and if you would like clarification for anything here, feel free to ask!):

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer

local currentPlayerConnections = {}

local button = script.Parent
local enabled = button:WaitForChild("Enabled")


local function UpdateNameTag(Character)
    local Head = Character:WaitForChild("Head")
    local Rank = Head:WaitForChild("Rank")

    if Head and Rank then
        Rank.Enabled = enabled.Enabled -- Sets the "Enabled" property of the nametag to the current value that was set for the "enabled" object. If it's true, then nametags are visible, and if it's false, then nametags are not visible.
    end
end

enabled:GetPropertyChangedSignal("Enabled"):Connect(function() -- Whenever the "Enabled" property of the "enabled" object changes, then...
    for _, player in Players:GetPlayers() do -- Loops through every player in the game
        local Character = player.Character or player.CharacterAdded:Wait()

        UpdateNameTag(Character) -- Sends the player's Character to the "UpdateNameTag" function
    end
end)

button.Activated:Connect(function()
    local currentValue = enabled.Enabled
    enabled.Enabled = not currentValue -- Swaps the "Enabled" property to the opposite of what it is now (meaning that it'll become `false` if it's `true` when the button is clicked, and vice versa)
end)

---

local function CreateEventConnections(player)
    local Character = player.Character or player.CharacterAdded:Wait()
    UpdateNameTag(Character) -- Runs the function with their existing Character model once to ensure that the value of the current setting will be respected (so that you won't need to wait until that player's Character respawns for the function to be called for the first time)

    if not currentPlayerConnections[player] then
        currentPlayerConnections[player] = player.CharacterAdded:Connect(UpdateNameTag)
-- This ensures the "UpdateNameTag" function will be updated for every player's Character each time anyone respawns. The event connection is stored so that it can be disconnected when the player leaves.
    end
end

for _, player in Players:GetPlayers() do
    CreateEventConnections(player)
end

Players.PlayerAdded:Connect(function(player)
    CreateEventConnections(player)
end)

Players.PlayerRemoving:Connect(function(player)
    currentPlayerConnections[player]:Disconnect()
    currentPlayerConnections[player] = nil
end)
1 Like

To toggle the Rank for characters that have died, you just need to add a listener.

Something like so:

local function PlayerAdded(player)	
	
	local function CharacterAdded(character)
		
		-- call function
		
		if player ~= LocalPlayer then -- if you don't want your own toggled
			ToggleRank(player)
		end

	end
	player.CharacterAdded:Connect(CharacterAdded)
	
end

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

Players.PlayerAdded:Connect(PlayerAdded)

This is assuming you put your current function (triggered by the button) into a named function so you can call it from more than one place. Something like so:

local function ToggleRank()
	
	if disabled.Enabled == true and enabled.Enabled == false then -- setting is disabled, client is trying to enable
		for i, plr in pairs(Players:GetChildren()) do
			local head = plr.Character:WaitForChild("Head")
			head:FindFirstChild("Rank").Enabled = false
		end

		enabled.Enabled = true 
		disabled.Enabled = false

		return
	end

	if enabled.Enabled == true and disabled.Enabled == false then -- setting is enabled, client is trying to disable
		for i, plr in pairs(Players:GetChildren()) do
			local head = plr.Character:WaitForChild("Head")
			head:FindFirstChild("Rank").Enabled = true
		end

		enabled.Enabled = false 
		disabled.Enabled = true

		return
	end
end

script.Parent.MouseButton1Click:Connect(function() ToggleRank() end)

Place this function above the CharacterAdded function.

That script will be inconsistent, check if the player already has a character, as that may be the case sometimes.

Thanks for the help, it worked out besides a small little confusion with what color was supposed to be showing at what time and a small bug but I managed to figure it out!

1 Like

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