Body Colors Reseting on Respawn

Hello,
I am making a game where the player can adjust their body colors using a gui button and it works but when the player resets or rejoins the game, it reverts back to the player’s original body colors. I want it to be so body colors stay if adjusted in-game even if the player dies or leaves the server.

Here is the script in the button that changes body color:

local imageButton = script.Parent

imageButton.MouseButton1Click:Connect(function()
	local player = game.Players.LocalPlayer
	local character = player.Character or player.CharacterAdded:Wait()

	if not character then
		warn("Character is missing!")
		return
	end

	-- Ensure the character parts exist
	local head = character:FindFirstChild("Head")
	local torso = character:FindFirstChild("Torso")
	local leftArm = character:FindFirstChild("Left Arm")
	local rightArm = character:FindFirstChild("Right Arm")
	local leftLeg = character:FindFirstChild("Left Leg")
	local rightLeg = character:FindFirstChild("Right Leg")

	if torso then torso.BrickColor = BrickColor.new(Color3.fromRGB(234, 190, 211)) end
	if leftLeg then leftLeg.BrickColor = BrickColor.new(Color3.fromRGB(228, 153, 179)) end
	if rightLeg then rightLeg.BrickColor = BrickColor.new(Color3.fromRGB(228, 153, 179)) end

	print("Body colors applied successfully!")
end)

I want the changed body color to stay even if the player resets or leaves the game and rejoin but no matter what I try, I cannot get it to work b/c it always ends up reverting to the original body colors.

This was my original solution to preventing player appearance from reseting on respawn if this helps (even though it doesn’t work):

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	local appearanceData = {}

	-- Store the player's appearance (mesh parts and accessories)
	local function storeAppearance(character)
		for _, part in pairs(character:GetChildren()) do
			if part:IsA("MeshPart") or part:IsA("Accessory") then
				appearanceData[part.Name] = part:Clone() -- Clone the parts and store them
			end
		end
	end

	-- Reapply the stored appearance
	local function applyAppearance(character)
		-- First, remove all existing parts from the new character
		for _, part in pairs(character:GetChildren()) do
			if part:IsA("MeshPart") or part:IsA("Accessory") then
				part:Destroy() -- Remove existing parts
			end
		end

		-- Reapply the stored parts (including accessories and mesh parts)
		for _, partClone in pairs(appearanceData) do
			partClone.Parent = character
		end
	end

	-- Ensure the player's appearance persists upon respawn
	player.CharacterAdded:Connect(function(character)
		appearanceData = {} -- Reset appearance data
		storeAppearance(character) -- Store the new appearance data

		-- Apply the stored appearance after character has fully loaded
		character:WaitForChild("HumanoidRootPart") -- Ensure the character is fully loaded
		applyAppearance(character)
	end)
end)

Let me know if your are able to help at all :slight_smile:

In the second script you have your CharacterAdded function reset the appearance data and save it into a table, then after they load their character it loads the empty table’s “data”. I think you need to move the storeAppearance() and resetting the appearance data stuff.

This should work although I haven’t tested it myself.

-- Services
local DataStoreService = game:GetService("DataStoreService")
local bodyColorsStore = DataStoreService:GetDataStore("PlayerBodyColors")
local Players = game:GetService("Players")

local imageButton = script.Parent

imageButton.MouseButton1Click:Connect(function()
    local player = game.Players.LocalPlayer
    local character = player.Character or player.CharacterAdded:Wait()

    if not character then
        warn("Character is missing!")
        return
    end

    -- Define the color values (example colors)
    local torsoColor = BrickColor.new(Color3.fromRGB(234, 190, 211))
    local legColor = BrickColor.new(Color3.fromRGB(228, 153, 179))

    -- Change the character's body parts colors
    local torso = character:FindFirstChild("Torso")
    local leftLeg = character:FindFirstChild("Left Leg")
    local rightLeg = character:FindFirstChild("Right Leg")

    if torso then torso.BrickColor = torsoColor end
    if leftLeg then leftLeg.BrickColor = legColor end
    if rightLeg then rightLeg.BrickColor = legColor end

    -- Save the color choices to DataStore
    local success, errorMessage = pcall(function()
        bodyColorsStore:SetAsync(player.UserId, {Torso = torsoColor, Legs = legColor})
    end)

    if success then
        print("Body colors saved!")
    else
        warn("Error saving body colors: " .. errorMessage)
    end
end)

-- Player Respawn Logic (loading the colors)
Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        -- Load the saved color data from DataStore
        local success, colorData = pcall(function()
            return bodyColorsStore:GetAsync(player.UserId)
        end)

        if success and colorData then
            -- Apply the saved colors to the character's body parts
            local torso = character:FindFirstChild("Torso")
            local leftLeg = character:FindFirstChild("Left Leg")
            local rightLeg = character:FindFirstChild("Right Leg")

            if torso and colorData.Torso then
                torso.BrickColor = colorData.Torso
            end
            if leftLeg and colorData.Legs then
                leftLeg.BrickColor = colorData.Legs
            end
            if rightLeg and colorData.Legs then
                rightLeg.BrickColor = colorData.Legs
            end
        else
            warn("No color data found for player " .. player.Name)
        end
    end)
end)