i have this panel on my game (Part > SurfaceGui > Frame > UIGradient) and there are a Click Detector on this part. i want to get the color when player are pressing at this part
already, i have this:
local tweenservice = game:GetService("TweenService")
local event = game.ReplicatedStorage.Event.Color
script.Parent.ClickDetector.MouseClick:Connect(function(plr)
local color = event:InvokeClient(plr)
task.wait(1)
print(color)
end)
local event = game.ReplicatedStorage.Event.Color
event.OnClientInvoke = function()
local mouse = game.Players.LocalPlayer:GetMouse()
local target = mouse.Target
if target and target:IsA("Part") then
local uiGradient = target:FindFirstChild("SurfaceGui"):FindFirstChild("TextLabel").UIGradient
if uiGradient then
local colors = uiGradient.Color
print(colors)
if colors then
return colors
end
end
end
print("no color")
end
but for sure it’s doesn’t work because this is returning a Color Sequence.
I need to make it return a single color, which the player clicked on
Unfortunately, I believe this is the expected behavior!
I think your best bet is to use a Mouse.Hit
the mouse clicks the part, then use some Vector math to map where on the part the player clicked on to a position in the ColorSequence
.
This assumes that your gradient is facing the Z axis. (If not, switch any mention of X with Z and Z with X.)
We can retrieve the color by using the percent from the left side the player clicked as “time” in the ColorSequence
. The full “time” of the ColorSequence
is 1, so if we think of the left side of the part as 0 and the right side as 1, we can find the color!
- On player click, check if Mouse.Target is the GUI. If the player is clicking on the GUI, get Mouse.Hit. This will tell you where the player is clicking on the GUI.
- Using the position and size of the GUI’s part, you can find the
topLeftCorner
of where the GUI is in 3D space. You can find how to do this here: How to find corners of part?
- Find the difference from the left side by getting the difference between the
topLeftCorner.X
and the mouse.Hit.Position.X
- Convert that to a percentage by dividing that number by the
target.Part.Size.X
.
- Use the
evalColorSequence
function here to finally get the color: ColorSequence | Documentation - Roblox Creator Hub (given the ColorSequence and the percentage from step 4)
Author’s note: writing instructions for this turned out to be a lot more complicated than I thought it was, so if it helps to have something clarified feel free to reach out. I’ll admit that vector math scrambles my brain.