Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

More proper/more efficient way of doing this script?

Asked by
roquick 41
5 years ago

Hi, I have 4 buttons in my gui, and I made them change colour when someone places their mouse over the button. The script works perfectly, but I was wondering, is there a more efficient/better way of doing this? because it seems so repetitive.

local ADF = script.Parent:WaitForChild('ADF')
 local AFW = script.Parent:WaitForChild('AFW')
 local AGK = script.Parent:WaitForChild('AGK')
 local AMF = script.Parent:WaitForChild('AMF')

ADF.MouseEnter:connect(function()
ADF.BackgroundColor3 = Color3.fromRGB(17, 135, 165)
end)
ADF.MouseLeave:connect(function()
ADF.BackgroundColor3 = Color3.fromRGB(22, 174, 214)
end)

AFW.MouseEnter:connect(function()
AFW.BackgroundColor3 = Color3.fromRGB(17, 135, 165)
end)
AFW.MouseLeave:connect(function()
AFW.BackgroundColor3 = Color3.fromRGB(22, 174, 214)
end)

AGK.MouseEnter:connect(function()
AGK.BackgroundColor3 = Color3.fromRGB(17, 135, 165)
end)
AGK.MouseLeave:connect(function()
AGK.BackgroundColor3 = Color3.fromRGB(22, 174, 214)
end)

AMF.MouseEnter:connect(function()
AMF.BackgroundColor3 = Color3.fromRGB(17, 135, 165)
end)
AMF.MouseLeave:connect(function()
AMF.BackgroundColor3 = Color3.fromRGB(22, 174, 214)
end)

1 answer

Log in to vote
1
Answered by
yellp1 193
5 years ago

I'm with you man. I feel some times that doing things like that are repetitive and not efficient but the only thing that I can think of is this and even so, it's still repetitive..

local ADF = script.Parent:WaitForChild('ADF')
local AFW = script.Parent:WaitForChild('AFW')
local AGK = script.Parent:WaitForChild('AGK')
local AMF = script.Parent:WaitForChild('AMF')

local function Enter(Obj)
    Obj.BackgroundColor3 = Color3.fromRGB(17,135,165)
end

local function Leave(Obj)
    Obj.BackgroundColor3 = Color3.fromRGB(22,174,214)
end

ADF.MouseEnter:connect(function()
    Enter(ADF)
end)
ADF.MouseLeave:connect(function()
    Leave(ADF)
end)

AFW.MouseEnter:connect(function()
    Enter(AFW)
end)
AFW.MouseLeave:connect(function()
    Leave(AFW)
end)

AGK.MouseEnter:connect(function()
    Enter(AGK)
end)
AGK.MouseLeave:connect(function()
    Leave(AGK)
end)
0
connect is deprecated it is Connect. User#19524 175 — 5y
0
I didn't use connect I just copied and pasted his code but yes good catch yellp1 193 — 5y
Ad

Answer this question