local t = 3
while wait() do
local hue = tick() % t / t
local color = Color3.fromHSV(hue,1,1)
game.Workspace.test.Color = color
end
this code is pretty hard to understand is there a way to make it less complex
local t = 3
while wait() do
local hue = tick() % t / t
local color = Color3.fromHSV(hue,1,1)
game.Workspace.test.Color = color
end
this code is pretty hard to understand is there a way to make it less complex
You mean you didn’t write this and want to know how it works?
tick() gets the current time in seconds. %t gets the remainder after dividing by 3. So 3%3 is 0, 4%3 is 1, 5%3 is 2, and 6%3 goes back to 0. Then divide by t to make the result a maximum of 1.
In the end, it causes a full rainbow every t seconds.
So it’s basically dividing tick by 0 cause 3 / 3 is 0
just fix the indenting and replace “wait” with “task.wait” to make it cleaner and maybe less throttling
Would doing task.wait improve performance or what’s the difference
read solution
3/3 is 1, not 0.
You can try messing around with math like that in the console or look up a better explanation on modular division, but the purpose of this code is to start at 0 and increase to 1 over the course of 3 seconds then wrap back around to 0 and start again.
Adding on to what @JarodOfOrbiter 3/3 is not 0, however, you are not doing the 3/3 to get the result (which would be 1) you are doing 3/3 and getting the remainder, and since 3 goes into 3 an even amount of times there is no remainder so it would be 0, here as an example on how to use % (modulo) to find if a number is a multiple if 5:
for n=1, 50 do
if n%5 == 0 then
print(n, "is a multiple of 5")
else
print(n, "is not a multiple of 5")
end
end
There is a difference between these 2 symbols : /
and %
.
/
= division [ 2/1 = 2].
%
= modulo, meaning if checks if the given number is a multiple of the given other number.
here is a video that explains the modulus operator
and here is another way to write the code
local runService = game:GetService("RunService")
-- value will keep increasing over time
local value = 0
-- speed effects how fast value will increase over time
local speed = 1
-- every heartbeat call this function (heartbeat is called after the physics simulation has completed)
runService.Heartbeat:Connect(function(deltaTime)
-- increase value using the deltaTime multiplied by the speed (deltaTime is how long has past since the previous heartbeat function was called)
value += deltaTime * speed
-- we now wrap the value around 1 watch the video above to see how the modules operator works
local hue = value % 1
-- make the color using the hue value
local color = Color3.fromHSV(hue, 1, 1)
-- set the test part to the color
game.Workspace.Test.Color = color
end)