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

Why is my Character Not Changing Colors?

Asked by
ruwuhi 23
3 years ago

I have a piece of code that is supposed to change the color of the character's limbs as seen below:

LocalScript (located under a textButton):

local button = script.Parent
local player = game:GetService("Players").LocalPlayer
local BodyColor = Color3.new(0,0,0)

button.MouseButton1Down:connect(function()

    print("hello")
    local ReplicatedStorage = game:GetService("ReplicatedStorage")
    local createPartEvent = ReplicatedStorage:WaitForChild("ChangeColorEvent")

    createPartEvent:FireServer(BodyColor)

end)

Server Script (located in ServerScriptService):

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local createPartEvent = Instance.new("RemoteEvent", ReplicatedStorage)
local character
createPartEvent.Name = "ChangeColorEvent"

local function onFired(player, BodyColor)

    character = player.CharacterAdded:Wait()
    character.Torso.Color = BodyColor
    character.LeftFoot.Color = BodyColor
    character.LeftHand.Color = BodyColor
    character.RightHand.Color = BodyColor
    character.RightFoot.Color = BodyColor
end

createPartEvent.OnServerEvent:Connect(onFired)

Although both events and functions are firing, the color of the character is not changing. Any ideas on why it isn't working?

3 answers

Log in to vote
1
Answered by 3 years ago

Well, I noticed a few things while looking at your code.

When you say

character = player.CharacterAdded:Wait()

You are essentially telling the code to wait for the character to respawn. A better option would be to select the current character. You could use the following to accomplish this:

character = player.Character or player.CharacterAdded:Wait()

This will tell the script to use the player's current character, or wait or one if one does not already exist.

Secondly, you are mixing up some limb names between R6 and R15. Torso is R6, while the rest you have listed are R15 body parts.

You can see an example of R6 body part names listed below: https://gyazo.com/c0886f8374ad06e189f3e80d99155cd7

This said, you have some options: 1) Fix your references to body parts 2) Loop through the entire character, searching for BaseParts and changing them.

After I made these changes within your script, I was able to make it work as expected.

If you need further help, feel free to join our Discord server!

Ad
Log in to vote
1
Answered by 3 years ago

A really easy way to do this is to create a HumanoidDescription and apply the corresponding Color3's to the parts associated with the Humanoid. (There is a list on the dev wiki that tells you the Color3 values of specific BrickColors. You can find that here.)

Applying BrickColors one by one is just too strenuous, and creating a custom HumanoidDescription makes this process so much easier. In order to apply the description to the Humanoid, you do what makes common sense: Humanoid:ApplyDescription().

-- example

local desc = script.Parent:FindFirstChild("ExampleDesc") -- This is a HumanoidDescription; note that this differs from the default description added into characters of players
script.Parent.Humanoid:ApplyDescription(desc)

Looking back at what you tried to do, that would take too many lines of code. It's really as easy as creating a description and applying it.

Log in to vote
0
Answered by 3 years ago

You can make your character finding a bit better by looking through the workspace for the character; theoretically, the character would have already loaded. The script would be

function onFired(player, character)
     local character = game.Workspace:FindFirstChild(player.Name)
     if character ~= nil then
          --Next code
     end
end

Also, you are combining r15 and r6. r6 does not have hands and r15 does not have a torso. If you need me to elaborate, please tell me.

I hope this answers your question.

Answer this question