Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

More proper/more efficient way of doing this script?

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

✓ Accepted answer

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)

2 comments

0

connect is deprecated it is Connect.

0

I didn't use connect I just copied and pasted his code but yes good catch

This discussion is preserved as it was. New answers, comments, and votes are closed.