Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I am trying to make a random colored head but it doesn't work?

I'm trying to make it so when the user joins, they will have a random colored head. Here's my script

local Player = game:GetService("Players")
Player.PlayerAdded:Connect(function(player)
	player.Character.Head.BrickColor = BrickColor.Random()
end)

2 answers

✓ Accepted answer

There is a typo on line 3, do this instead:

player.Character.Head.BrickColor = BrickColor.random()

remember that function calls are case-sensitive

edit: you should also do this every time the player's character is added because as it stands it will do one of two things:

  • give an error because the player is added before the character is
  • if the character exists, this will only happen once and if the person dies, resets their character, etc. their head will go back to normal

so try this:

local Player = game:GetService("Players")
Player.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
	character.Head.BrickColor = BrickColor.random()
	end)
end)

4 comments

0

it still doesn't work. It's a localscript in ServerScriptService.

0

LocalScripts run on the client side and do not work under ServerScriptService. Put this in a regular (server-side) script. You should use Scripts for things you want everyone in the game to see (so, most things), and LocalScripts are mainly for GUIs and other things that are unique to each person

0

i put a script in ServerScriptService and it still doesnt work

0

I tested this in Studio and it works when you put wait() before line 4, so I guess Roblox overrides the head's color when the character is initially created and that's why it doesn't work without the wait()

You need the BodyColor:

local Char = game.Players.LocalPlayer.Character
local BodyColors = Char:WaitForChild("BodyColors")

wait(0.1)
BodyColor.Head.BrickColor = BrickColor.random()

1 comment

0

thanks to you both!!!

This discussion is preserved as it was. New answers, comments, and votes are closed.