local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local num1 = math.random(255)
local num2 = math.random(255)
local num3 = math.random(255)
mouse.KeyDown:Connect(function(key)
if key == 'w' then
w.TextColor3 = Color3.new(num1, num2, num3)
else
if key == 'a' then
a.TextColor3 = Color3.new(num1, num2, num3)
else
if key == 's' then
s.TextColor3 = Color3.new(num1, num2, num3)
else
if key == 'd' then
d.TextColor3 = Color3.new(num1, num2, num3)
end
end
end
end
end)
mouse.KeyUp:Connect(function(key)
if key == 'w' then
w.TextColor3 = Color3.new(255, 255, 255)
else
if key == 'a' then
a.TextColor3 = Color3.new(255, 255, 255)
else
if key == 's' then
s.TextColor3 = Color3.new(255, 255, 255)
else
if key == 'd' then
d.TextColor3 = Color3.new(255, 255, 255)
end
end
end
end
end)
COMMUNITY QUESTION
I'm trying to make the text colour of labels change to a random colour every time they are pressed?
1 answer
We should take an example of how the code works.
You defined the random numbers as num1, num2 and num3. When you run the code your numbers will be some random ones. Let's take an example: 195, 211 and 59. you press s on your keyboard, and it changes the label's color from white to the color represented by the numbers. It works, but then you press s again, ans since the variables's values weren't modified it colors the labels with the same color as before!
To solve this problem, your code should be like this:
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local num1 = math.random(255)
local num2 = math.random(255)
local num3 = math.random(255)
mouse.KeyDown:Connect(function(key)
if key == 'w' then
w.TextColor3 = Color3.new(num1, num2, num3)
num1 = math.random(255)
num2 = math.random(255)
num3 = math.random(255)
else
if key == 'a' then
a.TextColor3 = Color3.new(num1, num2, num3)
num1 = math.random(255)
num2 = math.random(255)
num3 = math.random(255)
else
if key == 's' then
s.TextColor3 = Color3.new(num1, num2, num3)
num1 = math.random(255)
num2 = math.random(255)
num3 = math.random(255)
else
if key == 'd' then
d.TextColor3 = Color3.new(num1, num2, num3)
num1 = math.random(255)
num2 = math.random(255)
num3 = math.random(255)
end
end
end
end
end)
mouse.KeyUp:Connect(function(key)
if key == 'w' then
w.TextColor3 = Color3.new(255, 255, 255)
else
if key == 'a' then
a.TextColor3 = Color3.new(255, 255, 255)
else
if key == 's' then
s.TextColor3 = Color3.new(255, 255, 255)
else
if key == 'd' then
d.TextColor3 = Color3.new(255, 255, 255)
end
end
end
end
end)
The only difference is that this code changes the variables's values every time the color of the label is changed.
This discussion is preserved as it was. New answers, comments, and votes are closed.
