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.
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.
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)
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