I have a script in ServerScriptService that changes the colors of a players bodyparts depending on the data they have saved
local dss = game:GetService("DataStoreService")
local data = dss:GetDataStore("DataStore")
game.Players.PlayerAdded:connect(function(player)
local head_color = data:GetAsync(player.UserId .. "head_color")
local torso_color = data:GetAsync(player.UserId .. "torso_color")
local leftarm_color = data:GetAsync(player.UserId .. "leftarm_color")
local rightarm_color = data:GetAsync(player.UserId .. "rightarm_color")
local leftleg_color = data:GetAsync(player.UserId .. "leftleg_color")
local rightleg_color = data:GetAsync(player.UserId .. "rightleg_color")
player.CharacterAdded:connect(function(character)
local bodycolors = character:WaitForChild("Body Colors")
bodycolors.HeadColor = BrickColor.new(head_color)
bodycolors.TorsoColor = BrickColor.new(torso_color)
bodycolors.LeftArmColor = BrickColor.new(leftarm_color)
bodycolors.RightArmColor = BrickColor.new(rightarm_color)
bodycolors.LeftLegColor = BrickColor.new(leftleg_color)
bodycolors.RightLegColor = BrickColor.new(rightleg_color)
end)
end)
Everything about it is fine, except that the first time the player spawns, the script does not work and the colors of their bodyparts is what they have set in Roblox. Any time they load in again (they die and respawn), the script works perfectly fine.
I’ve tried changing CharacterAdded to CharacterAppearanceLoaded, but that function does not seem to wait for Body Colors unfortunately, as it doesn’t fix anything.
Oh wait, also I just noticed your problem. Don’t forget that Datastores yield, and the character probably already loaded before you could connect your event. Try this instead:
game.Players.PlayerAdded:connect(function(player)
local head_color = data:GetAsync(player.UserId .. "head_color")
local torso_color = data:GetAsync(player.UserId .. "torso_color")
local leftarm_color = data:GetAsync(player.UserId .. "leftarm_color")
local rightarm_color = data:GetAsync(player.UserId .. "rightarm_color")
local leftleg_color = data:GetAsync(player.UserId .. "leftleg_color")
local rightleg_color = data:GetAsync(player.UserId .. "rightleg_color")
function LoadBodyColors(character)
local bodycolors = character:WaitForChild("Body Colors")
bodycolors.HeadColor = BrickColor.new(head_color)
bodycolors.TorsoColor = BrickColor.new(torso_color)
bodycolors.LeftArmColor = BrickColor.new(leftarm_color)
bodycolors.RightArmColor = BrickColor.new(rightarm_color)
bodycolors.LeftLegColor = BrickColor.new(leftleg_color)
bodycolors.RightLegColor = BrickColor.new(rightleg_color)
end
if player.Character then
LoadBodyColors(player.Character)
end
player.CharacterAdded:Connect(LoadBodyColors)
end)