Respawn with the same chosen color

i want to make players respawn with the same color they chose from my gui script cuz everytime they die or reset the color goes back to the default.
i can’t find a way to do it.
i looked around for something like this but didn’t find something helpful.
here’s my script in ServerScriptService that applies colors to players through a gui:

 local remoteEvent = game.ReplicatedStorage:WaitForChild("ColorChange")

remoteEvent.OnServerEvent:Connect(function(player, color)
	for i, part in pairs(player.Character:GetChildren()) do
		if part:IsA("BasePart") then
			part.BrickColor = BrickColor.new(color)
		end
	end
end)

i think it’s simple and i’m missing something but idk.

I’m not sure what you specifically want here, do you want the player to always keep the color they’ve chose?

yes exactly, i want them to always respawn with the same color they chose

So even when they leave the game?

ye it’ll be even better
but not necessarily if it’s harder

In that case, you should make a datastore, detect when a player joins and make a new string value named “playerColor”, make the parent the player that joined and check the datastore to see if the player has a color value or not.

can you show me how it’ll look like in codes since i’m not familiar with datastores and i’m new to coding

You should go and watch some tutorials and go learn the API then. If your new to coding, I recommend you smart small.

looked into that and it kinda hard and messy, especially cuz i’m using BrickColor on a humanoid, so what if i just want them to just respawn with the same color they chose? not necessarily when they leave the game

Add a .CharactedAdded so it respawns them with the same color.

Fixed code:

--//Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--//Variables
local remoteEvent = ReplicatedStorage:WaitForChild("ColorChange")

--//Tables
local Connections = {}

--//Functions
local function OnCharacterAdded(character, color)
	for i, part in ipairs(character:GetChildren()) do
		if part:IsA("BasePart") then
			part.BrickColor = BrickColor.new(color)
		end
	end
end

remoteEvent.OnServerEvent:Connect(function(player, color)
	local colorConnection = Connections[player]
	
	if colorConnection then
		colorConnection:Disconnect()
		colorConnection = nil
	end

	Connections[player] = player.CharacterAdded:Connect(function(character)
		OnCharacterAdded(character, color)
	end)
	
	OnCharacterAdded(player.Character, color)
end)
1 Like

thanks for helping, but i already found help on discord.
here’s the code that worked for me for anyone wondering:

local remoteEvent = game.ReplicatedStorage:WaitForChild("ColorChange")

local playerColor = {}

remoteEvent.OnServerEvent:Connect(function(player, color)
	for i, part in pairs(player.Character:GetChildren()) do
		if part:IsA("BasePart") then
			part.BrickColor = BrickColor.new(color)
		end
	end

	playerColor[player] = color
end)

local function onCharacterAdded(character)
	local player = game.Players:GetPlayerFromCharacter(character)
	if player then
		local color = playerColor[player]
		if color then
			for i, part in pairs(character:GetChildren()) do
				if part:IsA("BasePart") then
					part.BrickColor = BrickColor.new(color)
				end
			end
		end
	end
end

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		onCharacterAdded(character)
	end)
end)

for _, player in ipairs(game.Players:GetPlayers()) do
	if player.Character then
		onCharacterAdded(player.Character)
	end
end

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