How to change a selectionbox's color from red to black in a server script? (not working when i try)

im trying to animate a selectionbox’s color to transition from red to black then black to red. i thought about changing the HSV’s value from 1 to 255 then 255 to 1 in a while loop but roblox simply cannot understand lua as it is not very bright and taking special ED classes in kindagarden. when my code simply works but roblox doesnt want to run it is what makes me mad.

local selection = Instance.new("SelectionBox")
selection.Adornee = script.Parent
selection.Parent = script.Parent
selection.LineThickness = 0.1
selection.Color3 = Color3.new(1, 0, 0.0156863)

while wait(0.1) do
	wait(0.1)
	for i=255,0,-1 do
		wait(0.1)
		local h,s,v = selection.Color3:ToHSV()
		v = v - 1
		local new = Color3.fromHSV(h,s,v)
		selection.Color3 = new
	end
	wait(0.1)
	for i=0,255,1 do
		wait(0.1)
		local h,s,v = selection.Color3:ToHSV()
		v = v + 1
		local new = Color3.fromHSV(h,s,v)
		selection.Color3 = new
	end
end

image

this was the code i tried yet roblox was unable to run the code as it didnt know what was going on and was confused. poor Roblox’s fragile brain!

Why don’t you just use TweenService?

local tweenService = game:GetService("TweenService")

while true do
    local tweenInfo = TweenInfo.new( -- new tweeninfo object
         0.1 * 255, -- the amount of time the tween takes
         Enum.EasingStyle.Linear,  -- tween style
         Enum.EasingDirection.InOut, -- tween direction
         0, -- repeat count
         true, -- return to the original value
         0 -- delay time
     )

    local tween = tweenService:Create(selection, tweenInfo, {Color3 = Color3.fromRGB()})
    -- create a new tween instance
    tween:Play() -- play the tween
    tween.Completed:Wait() -- wait for it to end
end

https://developer.roblox.com/en-us/api-reference/datatype/TweenInfo

2 Likes

i didnt know i could tween colors but thank you. i will do more research on tweening with colors

h,s,v in :ToHSV() and .fromHSV() are measured in percentages of 1.