Help tweening a parts color to make it darker

Hello, I have an issue where I want to darken the color of a part by a percentage of a given number value.

Basically, when a dough is being cooked, a server script increases a number value by 1% every 0.1 seconds. I want to use this value to gradually change the color of the dough to a brownish shade.

Here is a working script, although it’s rought:

local info = script.Parent:WaitForChild("info")
local handle = script.Parent:WaitForChild("Handle")
local val = info:WaitForChild("Cooked")
local stat = info:WaitForChild("Status")

game:GetService("RunService").Heartbeat:Connect(function()
	if val.Value == 100 then
		handle.Color = Color3.new(0.352941, 0.298039, 0.258824) -- Main color it should be aiming at.
		stat = 'Cooked'
	elseif val.Value > 130 then
		handle.Color = Color3.new(0, 0, 0) -- Burned
		stat = 'Burned'
	elseif val.Value < 100 then
		handle.Color = Color3.new(0.843137, 0.772549, 0.603922) -- Raw color or starting color
		stat = 'Raw'
	end
end)

Workspace tree if needed:
image

You would Interpolate the Color towards the value, You can use Color3:Lerp() if needed, as TweenService doesnt really seem nessecary for that many Tweens or for that time, and Color3:Lerp() is more controllable.

So You can say this:

local RawColor = Color3.new(0.843137, 0.772549, 0.603922)
local CookedColor = Color3.new(0.352941, 0.298039, 0.258824)

local InterpolatedColor = RawColor:Lerp(CookedColor, .01) -- 1% would be equal to .01

But if you want it to be burned, you would need it to reach the full color before burning it, if thats what youre trying to do.

How would i implement this into the script cause i cant really figure it out lol.

Im gonna remark the things that do and when they do by showing some parts of the scripts.

image

The purple arrow points to a Number Value that determines the cook percentage.

The Yellow arrow is the Server Script that acts as the color changer for the Handle and the script i provided in my post.

The red arrow points to a Server Script that adds each percent every .1sec when the Handle is touching a part and being cooked.

script.Parent:WaitForChild("RemoteEvent").OnServerEvent:Connect(function(plr)
	script.Parent:WaitForChild("info"):WaitForChild("Cooked").Value += 1
	print("adding")
	wait()
end)

The blue arrow is the main Local Script and fires a Remote Event when the Handle is touching a HitBox.

wait()

cookin = false

script.Parent:WaitForChild("Handle").Touched:Connect(function(t)
	if t.Name == 'HitBox' and t.Parent.Name == 'Furnace' then
		cookin = true
		print("true")
		while cookin == true do
			script.Parent:WaitForChild("RemoteEvent"):FireServer()
			print("firing server")
			wait(.1)
		end
	end
end)

script.Parent:WaitForChild("Handle").TouchEnded:Connect(function(t)
	if t.Name == 'HitBox' and t.Parent.Name == 'Furnace' then
		cookin = false
		print("not true")
	end
end)

If you’re going to use RunService.Heartbeat to change the Color of the Object, you may be able to have something like this, where it will smoothley transition between a Lighter Color, and a Darker color

local Raw = Color3.fromRGB(215, 197, 154) -- Start Color
local Full = Color3.fromRGB(90, 76, 66) -- Fill Color

local Duration = 10 -- Time it takes to cook
local Current  = Duration -- The Current Time left
local Lerp     = 0 -- Used for Lerping and Percentage


RunService.Heartbeat:Connect(function(dt)
	Current = math.max(0, Current - dt) -- dt would be roughly equal to 0.01 or less
    -- math.max will prevent the value from going below 0
	Lerp = (Duration - Current)/Duration -- The Point the Interpolation will be at
    -- Because Current is getting Lower, it will actually add towards the full Duration
    -- Divided by Duration would have the Value be between 0 and 1, which is what Lerp accepts
    -- in Total, should take roughly 10 seconds
    -- 'Lerp' (the variable) can also be used as a Percentage Value if needed
	
	Object.Color = Raw:Lerp(Full, Lerp) -- Lerps using Data
end)

Setting to duration for longer should have it for longer, since you want it to change every .1 seconds, every 1 second would be .1 to mark 10%, and 10 seconds would be exactly 1, as 100%, It will never be exact however.

Not really what i had in mind but thank you anyways! :Dd

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