Made a script that changes button colors based on scroll bar position. I am currently using Run Service to update it. Is there a more effective way to update it?
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")
local ScrollingFrame = script.Parent.Main.ScrollingFrame
local pos1end = 1109
local pos2end = 2000
local pos3end = 3500
local pos4end = 4439.5
local pos1start = 0
local pos2start = 1110
local pos3start = 2001
local pos4start = 3501
local Tween1 = TweenService:Create(ScrollingFrame, TweenInfo.new(0.25), {CanvasPosition = Vector2.new(pos1start, 0)})
local Tween2 = TweenService:Create(ScrollingFrame, TweenInfo.new(0.25), {CanvasPosition = Vector2.new(pos2start, 0)})
local Tween3 = TweenService:Create(ScrollingFrame, TweenInfo.new(0.25), {CanvasPosition = Vector2.new(pos3start, 0)})
local Tween4 = TweenService:Create(ScrollingFrame, TweenInfo.new(0.25), {CanvasPosition = Vector2.new(pos4start, 0)})
local LightBlue = Color3.fromRGB(0, 170, 255)
local DarkBlue = Color3.fromRGB(0, 118, 177)
local White = Color3.fromRGB(255, 255, 255)
local Grey = Color3.fromRGB(177, 177, 177)
local button1 = script.Parent.Main.Topbar["1"]
local button2 = script.Parent.Main.Topbar["2"]
local button3 = script.Parent.Main.Topbar["3"]
local button4 = script.Parent.Main.Topbar["4"]
local function updateButtonColors()
local xPos = ScrollingFrame.CanvasPosition.X
if xPos <= pos4end and xPos >= pos3end then
button4.BackgroundColor3 = LightBlue
button3.BackgroundColor3 = White
button2.BackgroundColor3 = White
button1.BackgroundColor3 = White
button4.UIStroke.Color = DarkBlue
button3.UIStroke.Color = Grey
button2.UIStroke.Color = Grey
button1.UIStroke.Color = Grey
button4.TextLabel.UIStroke.Color = DarkBlue
button3.TextLabel.UIStroke.Color = Grey
button2.TextLabel.UIStroke.Color = Grey
button1.TextLabel.UIStroke.Color = Grey
elseif xPos <= pos3end and xPos >= pos2end then
button4.BackgroundColor3 = White
button3.BackgroundColor3 = LightBlue
button2.BackgroundColor3 = White
button1.BackgroundColor3 = White
button4.UIStroke.Color = Grey
button3.UIStroke.Color = DarkBlue
button2.UIStroke.Color = Grey
button1.UIStroke.Color = Grey
button4.TextLabel.UIStroke.Color = Grey
button3.TextLabel.UIStroke.Color = DarkBlue
button2.TextLabel.UIStroke.Color = Grey
button1.TextLabel.UIStroke.Color = Grey
elseif xPos <= pos2end and xPos >= pos1end then
button4.BackgroundColor3 = White
button3.BackgroundColor3 = White
button2.BackgroundColor3 = LightBlue
button1.BackgroundColor3 = White
button4.UIStroke.Color = Grey
button3.UIStroke.Color = Grey
button2.UIStroke.Color = DarkBlue
button1.UIStroke.Color = Grey
button4.TextLabel.UIStroke.Color = Grey
button3.TextLabel.UIStroke.Color = Grey
button2.TextLabel.UIStroke.Color = DarkBlue
button1.TextLabel.UIStroke.Color = Grey
elseif xPos <= pos1end and xPos >= 0 then
button4.BackgroundColor3 = White
button3.BackgroundColor3 = White
button2.BackgroundColor3 = White
button1.BackgroundColor3 = LightBlue
button4.UIStroke.Color = Grey
button3.UIStroke.Color = Grey
button2.UIStroke.Color = Grey
button1.UIStroke.Color = DarkBlue
button4.TextLabel.UIStroke.Color = Grey
button3.TextLabel.UIStroke.Color = Grey
button2.TextLabel.UIStroke.Color = Grey
button1.TextLabel.UIStroke.Color = DarkBlue
end
end
RunService.RenderStepped:Connect(updateButtonColors)
button1.MouseButton1Click:Connect(function()
Tween1:Play()
end)
button2.MouseButton1Click:Connect(function()
Tween2:Play()
end)
button3.MouseButton1Click:Connect(function()
Tween3:Play()
end)
button4.MouseButton1Click:Connect(function()
Tween4:Play()
end)