Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Is there a more efficient way of using this lerp script? (I'm bad with lerp)

Just want to shorten this, and make it more efficient for future use.

while true do
	for i = 0,2,0.01 do
		script.Parent.BackgroundColor3 = script.Parent.BackgroundColor3:lerp(Color3.new(255/255, 26/255, 225/255), i / 100)
		wait()
	end
	for i = 0,2,0.01 do
		script.Parent.BackgroundColor3 = script.Parent.BackgroundColor3:lerp(Color3.new(30/255, 41/ 255, 255/255), i / 100)
		wait()
	end
	for i = 1,2,0.01 do
		script.Parent.BackgroundColor3 = script.Parent.BackgroundColor3:lerp(Color3.new(43/255, 170/255, 255/255), i / 100)
		wait()
	end
	for i = 1,2,0.01 do
		script.Parent.BackgroundColor3 = script.Parent.BackgroundColor3:lerp(Color3.new(43/255, 255/255, 50/255), i / 100)
		wait()
	end
	for i = 1,2,0.01 do
		script.Parent.BackgroundColor3 = script.Parent.BackgroundColor3:lerp(Color3.new(245/255, 255/255, 46/255), i / 100)
		wait()
	end
	for i = 1,2,0.01 do
		script.Parent.BackgroundColor3 = script.Parent.BackgroundColor3:lerp(Color3.new(255/255, 0/255, 0/255), i / 100)
		wait()
	end
end

1 answer

✓ Accepted answer

Whenever you see code that looks almost exactly the same, you can know that it's time to use a function.

Also, I'm going to assume you meant to have "for i = 0, 1, 0.01", and then just put 'i' in as the 2nd argument. However you do it, the second argument in lerp must be between 0 and 1.

Also, you might want to use Color3.fromRGB for convenience.

function changeColorTo(targetColor)
	for i = 0, 1, 0.01 do
		script.Parent.BackgroundColor3 = script.Parent.BackgroundColor3:lerp(targetColor, i)
		wait()
	end
end

while true do
	changeColorTo(Color3.fromRGB(255, 26, 225))
	--etc
end

When making your own function, you put as arguments anything that changes between the duplicate code. Now, I assumed that you actually wanted all the for loops to run the same way. Say you wanted some of them to start and end at different 'i' values, and maybe even have different step values. Then you would add an argument to the function for each value you want to be able to change. ex, it might look like

function changeColorTo(targetColor, start, goal, step)

You would change the for loop as well. Now, perhaps you want to give a default value to each argument, you'd first do this:

function changeColorTo(targetColor, start, goal, step)
	start = start or 0
	goal = goal or 1
	step = step or 0.01
	for i = start, goal, step do
		--etc

--Call the function with or without values for 'start', 'goal', etc. If you don't provide a value, that argument will be 'nil'. When you say "start = start or 0", if start is nil, this evaluates to "start = nil or 0", which will then give you the '0'. Note that this doesn't work if 'start' is 'false', but 'start' is never supposed to be given 'false', so that's okay.

This discussion is preserved as it was. New answers, comments, and votes are closed.