I'm working on a script that changes a player's colors to a random color every time they spawn. I have tried many variants, none of them having worked. Here is the latest variant.
--This is a LocalScript player = game.Players.LocalPlayer player.CharacterAdded:connect(function(character) player:WaitForDataReady() local color = BrickColor.Random() local head = character["Head"] local torso = character["Torso"] local rightarm = character["Right Arm"] local leftarm = character["Left Arm"] local rightleg = character["Right Leg"] local leftleg = character["Left Leg"] head.BrickColor = color torso.BrickColor = color rightarm.BrickColor = color leftarm.BrickColor = color rightleg.BrickColor = color leftleg.BrickColor = color end)
Could somebody please point out what the problem with my script is?
There are three problems here:
WaitForDataReady
has to do with Data Persistence and should not be used here, Body Colors
overwrites the changes you want to make so you'll have to get rid of that first, and this likely won't run the first time because the character will (probably) load before the script is ran.
A better way to do this would be in a server script:
game.Players.PlayerAdded:connect(function(player) player.CharacterAdded:connect(function(character) character:WaitForChild("Body Colors"):Destroy() local color = BrickColor.Random() character["Head"].BrickColor = color character["Torso"].BrickColor = color character["Right Arm"].BrickColor = color character["Left Arm"].BrickColor = color character["Right Leg"].BrickColor = color character["Left Leg"].BrickColor = color end) end)
Sometimes when the character is spawned, the model may be there, but the parts aren't. Use the CharacterAppearanceLoaded
event.
game.Players.PlayerAdded:connect(function(plr) plr.CharacterAppearanceLoaded:connect(function(chr) local color = BrickColor.random() for _,v in next, chr:GetChildren() do if v:IsA("Part") then v.BrickColor = color end end end) end)