Problem with color changing

I want a shirt and pants to change color to black gradually with “for loop”, but when i touch the trigger shirt changes color to black instantly and same applies to pants. Any ideas how to fix it?

code:

local part = script.Parent
local beata = game.Workspace:WaitForChild("Beata11")
local shirt = beata:FindFirstChildOfClass("Shirt")
local pants = beata:FindFirstChildOfClass("Pants")
local ShirtColor = shirt.Color3
local PantsColor = pants.Color3
local flag = true

local function PantsBlacken()
    local newColor = PantsColor
    while newColor.r > 0 or newColor.g > 0 or newColor.b > 0 do
        newColor = Color3.new(newColor.r - 1, newColor.g - 1, newColor.b - 1)
        pants.Color3 = newColor
        task.wait(0.5) -- wait for 0.5 seconds
    end
end

local function ShirtBlacken()
    local newColor = ShirtColor
    while newColor.r > 0 or newColor.g > 0 or newColor.b > 0 do
        newColor = Color3.new(newColor.r - 1, newColor.g - 1, newColor.b - 1)
        shirt.Color3 = newColor
        task.wait(0.5) -- wait for 0.5 seconds
    end
end

part.Touched:Connect(function(hit)
    local char = hit.Parent
    local players = game:GetService("Players")
    local player = players:GetPlayerFromCharacter(char)
    local hum = char:FindFirstChildOfClass("Humanoid")
    if hum and player and flag then
        flag = not flag
        print(char.Name .. "touched part")
        ShirtBlacken()
        PantsBlacken()
    end
end)
1 Like

I am not really sure why you are doing it this way when you can use Tween Service to do it, but if that is the only approach you’d like to take, the reason why it is instantly turning black instead of gradually changing its color, is because of this…

Instead of Color3.new, use Color3.FromRGB .

  • The difference is Color3.new goes from 0 to 1 for each Red, Green and Blue… This is the reason it is instantly turning black for you!

  • Whereas Color3.FromRGB goes from 0 to 255 for the three colors.

Edit: If you’d like to use Color3.new, then since it goes from 0 to 1, instead of decrementing by 1, decrement it by 0.01 or something.

Example 1, this uses Color3.fromRGB:

local function ShirtBlacken()
  local newColor = ShirtColor
  while newColor.r > 0 or newColor.g > 0 or newColor.b > 0 do
    newColor = Color3.fromRGB(newColor.r - 1, newColor.g - 1, newColor.b - 1)
    shirt.Color3 = newColor
    task.wait(0.5) -- wait for 0.5 seconds
  end
end

Example 2, this uses Color3.new:

local function ShirtBlacken()
  local newColor = ShirtColor
  while newColor.r > 0 or newColor.g > 0 or newColor.b > 0 do
    newColor = Color3.new(newColor.r - 0.05, newColor.g - 0.05, newColor.b - 0.05)
    shirt.Color3 = newColor
    task.wait(0.5) -- wait for 0.5 seconds
  end
end

Example 3, this uses TweenService:

--up here, declare a reference to TweenService for its methods/functions
local tweenService = game:GetService("TweenService")

local function ShirtBlacken()
  --create a tween
  -- tween Service's CREATE method takes in three parameters.
  --- The first one, is the instance you want to animate.
  --- The second one is the information, like duration, and easing styles of the tween.
------ The third one takes a table, there you can write all its goal "properties", you can write as many properties as you would wish to!
-------- In the third parameter, you don't need to write the Instance's name

-- this tween takes 10 seconds to reach its goals in the third parameter, which is a table
local tween = tweenService:Create(shirt, TweenInfo.new(10), { Color3 = Color3.new(0, 0, 0) } )

--After the tween is created, it has another method, and it's called "Play"

tween:Play()
end
1 Like

Thank you so much for such a detailed feedback. That is exactly what i required. Can’t express how much you help me!

2 Likes

You’re welcome! Glad I could help. Feel free to ask if you have any other questions. Keep creating! :slight_smile:

1 Like

If @Bpandu’s post helped you, please mark it as the solution.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.