How to change a body part color with a TextButton?

im creating a simulator and i need help making a script
how could i change a body part color with a textbutton?
example of what i would like to achieve: change torso color to blue

This is fairly simple to do, first of all: is your game R6 of R15? And which body part would you like to give a color?

Local script in the text button:

script.Parent.MouseClick:Connect(function()
       local char = YOUR_PLAYER_HERE.Character
       if char:FindFirstChild("Humanoid") then
              if char.Humanoid.Health >=1 then
                     char["Left Arm"].Color = Color3.FromRGB(255, 255, 255)
              end
       end
end)

Replace YOUR_PLAYER_HERE by the player.

Exemple to get the player:

Random player

Local plrs = game.Players
local RandomPlayerChosen = math.random(1, #plrs)

You (Local Player)

Local Player = game.Players.LocalPlayer

Target player

local TargetPlayer = game.Players.JustABaconHair1234

Exemple of R15 Body parts

image

Have a good day :smiley:

This has an issue, this only changes the body color for the client, since it’s in a local script, other people won’t see this. You would have to make use of RemoteEvents in order to make this work.

Also, mouseclick is for clickdetectors, in the case of a button, you use mousebutton1click.

This should work:

Create a localscript in the button
Start by adding a localscript in your button, let’s now script it.

First of all, we want to detect if a player has pressed the button, we can do this by using a mousebutton1click event.

script.Parent.MouseButton1Click:Connect(function()

end)

This event would fire when the player has clicked the button, we may also want to add a .3 second cooldown, to prevent the script from breaking.

We can do this by making a new variable called CoolDown, we will set this to false on standard. After that, we will check if the CoolDown is on false, if so, the script continue’s, if the CoolDown is on true, it doesn’t run the code.

We also have to set the CoolDown to true when the player clicked, then wait 0.3 seconds before we set it to false again.

Like this:

local coolDown = false

script.Parent.MouseButton1Click:Connect(function() -- The player has clicked the button
    if coolDown == false then -- If the cooldown is false, run the code

          coolDown = true -- Set the cooldown to true

          wait(.3) -- Wait 0.3 seconds

          coolDown = false -- Set the coolDown to false again

     end
end)

Great! The last thing to do is to fire the remote event to the server, so we can change the player’s color, we’re firing an event to the server, because if we change the players color on the client, only the client will see the color change, and not the other players.

So, go to ReplicatedStorage and add a RemoteEvent, call this event “BodyColorEvent”

Now, in our code we will paste the following:

game.ReplicatedStorage.BodyColorEvent:FireServer()

We’re done on the client side! Now we can script on the server side. First of all, add a script into ServerScriptService, you may call this whatever you want.

Now, we will detect when the event has been fired, type this code into your script.

game.ReplicatedStorage.BodyColorEvent.OnServerEvent:Connect(function()
      
end)

This code will get connected once the BodyColorEvent has been fired from the local script.

Now, we will get the player and the player’s character.

game.ReplicatedStorage.BodyColorEvent.OnServerEvent:Connect(function(player) -- The argument "player" is the player that got the event, so the player who fired the event from the local script

     local character = player.Character -- The player's character 

     if character then -- If the player isn't death and the character has been loaded in

     end

end)

With this, we first of all get the player by the first argument, then we can get the player’s character by that along with checking if the character is actually there.

Now, we can finally change the player’s color.

character["LowerTorso"].BrickColor = BrickColor.new("Really blue")
character["UpperTorso"].BrickColor = BrickColor.new("Really blue")

Assuming your game type is R15 and you would like to change the torso’s color, you have to change the lower and upper torso’s color, when you use R6, you can just say “Torso”.

If you would like to change the color, change the brickcolor to a different brickcolor, if you would like to give a different body part a different color, change the bodypart where I for now placed LowerTorso and UpperTorso.

If you followed all of these steps, this should be your final code:

LocalScript

local coolDown = false

script.Parent.MouseButton1Click:Connect(function() -- The player has clicked the button
    if coolDown == false then -- If the cooldown is false, run the code

          coolDown = true -- Set the cooldown to true

          game.ReplicatedStorage.BodyColorEvent:FireServer()

          wait(.3) -- Wait 0.3 seconds
          coolDown = false -- Set the coolDown to false again

     end
end)

ServerScript

game.ReplicatedStorage.BodyColorEvent.OnServerEvent:Connect(function(player) -- The argument "player" is the player that got the event, so the player who fired the event from the local script

      local character = player.Character -- The player's character 

      if character then -- If the player isn't death and the character has been loaded in
         character["LowerTorso"].BrickColor = BrickColor.new("Really blue")
         character["UpperTorso"].BrickColor = BrickColor.new("Really blue")
      end

 end)

Note: I didn’t test this yet, since I’m on a laptop without studio on it, if you have any errors, feel free to let me know so I can help you with it.

I hope this helped you!

3 Likes

game is r6
more details: my game has a startercharacter, since the head is modified and ive been trying to somewhat make a simple script that can achieve that but since i suck i wasnt lucky

update: your script seems to work excellent. thanks!

1 Like