So basically I’m wanting to randomize player’s hair color and save the color so that when the player joins again, they’d get the same hair color they got the first time they joined. But some really weird things happened when I tested the script below. Please help.
local hair_colors = {
"Crimson";
"Lime green";
"Deep Sea";
"Deep Orange";
"Dark stone grey";
"Really black";
"Lily white";
"Brown";
"Lavender";
"Bright Voilet";
}
local new_color = hair_colors[math.random(1,#hair_colors)]
local DataStore = game:GetService("DataStoreService")
local PlayerInfoTable = {}
local rgkData = DataStore:GetDataStore("RogueGaKillTest")
game.Players.PlayerAdded:Connect(function(player)
local data = Instance.new("Folder")
data.Name = "Data"
data.Parent = player
local loadData
local clan = Instance.new("IntValue")
clan.Parent = data
clan.Name = "Clan"
local strength = Instance.new("IntValue")
strength.Parent = data
strength.Name = "Strength"
local stamina = Instance.new("IntValue")
stamina.Parent = data
stamina.Name = "Stamina"
local hunger = Instance.new("IntValue")
hunger.Parent = data
hunger.Name = "Hunger"
local speed = Instance.new("IntValue")
speed.Parent = data
speed.Name = "Speed"
local defense = Instance.new("IntValue")
defense.Parent = data
defense.Name = "Defense"
local special = Instance.new("IntValue")
special.Parent = data
special.Name = "Special"
local hairColor = Instance.new("StringValue")
hairColor.Parent = data
hairColor.Name = "HairColor"
local success, err = pcall(function()
loadData = rgkData:GetAsync(player.UserId)
end)
if success then
if type(loadData) == "table" then
clan.Value = loadData[1] or 1
strength.Value = loadData[2] or 0
stamina.Value = loadData[3] or 0
speed.Value = loadData[4] or 0
defense.Value = loadData[5] or 0
special.Value = loadData[6] or 0
hunger.Value = loadData[7] or 100
hairColor.Value = loadData[8] or new_color
else
clan.Value = 1
strength.Value = 0
stamina.Value = 0
speed.Value = 0
defense.Value = 0
special.Value = 0
hunger.Value = 100
hairColor.Value = new_color
end
end
--MAIN HAIR COLOR LOADER
print(hairColor.Value)
print(new_color)
player.CharacterAppearanceLoaded:Connect(function(char)
for _,v in pairs(char:GetChildren()) do
if v:IsA("Accessory") then
if v.Handle:FindFirstChild("HairAttachment") ~= nil then
local mesh = v.Handle:FindFirstChild("Mesh") or v.Handle:FindFirstChild("SpecialMesh")
if mesh then
mesh.TextureId = ""
print("Removed Texture")
end
v.Handle.BrickColor = BrickColor.new(hairColor.Value)
print("Applied new hair color")
end
end
end
end)
PlayerInfoTable[player.UserId] = {
clan.Value,
strength.Value,
stamina.Value,
speed.Value,
defense.Value,
special.Value,
hunger.Value,
hairColor.Value
}
clan.Changed:Connect(function(value)
PlayerInfoTable[player.UserId][1] = value
end)
strength.Changed:Connect(function(value)
PlayerInfoTable[player.UserId][2] = value
end)
stamina.Changed:Connect(function(value)
PlayerInfoTable[player.UserId][3] = value
end)
speed.Changed:Connect(function(value)
PlayerInfoTable[player.UserId][4] = value
end)
defense.Changed:Connect(function(value)
PlayerInfoTable[player.UserId][5] = value
end)
special.Changed:Connect(function(value)
PlayerInfoTable[player.UserId][6] = value
end)
hunger.Changed:Connect(function(value)
PlayerInfoTable[player.UserId][7] = value
end)
hairColor.Changed:Connect(function(value)
PlayerInfoTable[player.UserId][8] = value
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
local data = PlayerInfoTable[player.UserId]
if data then
rgkData:UpdateAsync(player.UserId, function(oldData) return data end)
end
end)
Edit: The problem is how the hair loader does not load the color. I still can’t find out why…