Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to define the character's head?

Asked by 4 years ago

So I am trying to make a 2d platformer where the character can change colour by pressing certain keys. I am currently trying to figure out how to find the character as game.Players.LocalPlayer.Character says it's a nil value. I then went to the Dev Hub and found the character page. I changed to a local script and changed to player.Character and still no luck as player is unknown. Here is a small part of the code I am working with:

if player.Character.Head.BrickColor == BrickColor.new("Really red") then
    script.Parent.BrickColor = BrickColor.new("Really red")
end

I also have the code in a LocalScript inside of a Part in workspace. Any sort of help would be appreciated!

0
game.Players.LocalPlayer.Character is a nil value because you have to do game.Players.LocalPlayer:WaitForChild("Character") cucucu0001 35 — 4y
0
@cucucu0001 Bro, no. All LocalScripts are loaded after a player is loaded, there's no need to wait for the Character to load guest_20I8 266 — 4y

3 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

You cannot put a LocalScript in a part in workspace. Put the LocalScript in StarterPack and define part (e.g. local Part = game.Workspace.Part). The Player can be called by LocalScript in StarterPack using game.Players.LocalPlayer or script.Parent.Parent. Also if you want to detect a player's head color, you cannot use BrickColor.new("really red") because the color is not changed.

if player.Character.Head.BrickColor then
    script.Parent.BrickColor = BrickColor.new("Really red")
end

Let me explain the code above and why is it like that. So i assumed you have already set the player's head color to really red, or maybe you are going to change it in a script. If you did not, you will have to change the player's head color yourself. So explanation: if the player's head color is "Really Red" (if player.Character.Head.BrickColor then), then the Part's color will change, does that make sense to you?

Ad
Log in to vote
0
Answered by 4 years ago

This is simple! Just use this!

for i,v in pairs(game.Players:GetChildren()) do
if v.Character and v.Character:FindFirstChild("Head") then
{CODE}
end

Log in to vote
0
Answered by 4 years ago

LocalScripts that are cloned from StarterGui or StarterPack into a player’s Backpack or PlayerGui are often run before the old Character model is deleted. You're supposed to do:

if not character or not character.Parent then
    character = game.Players.LocalPlayer.CharacterAdded:wait()
end

Then you define character after the if statement. I hope this helps :)

Answer this question