Reducing amount of scripts

So I have this script that makes an effect pop up every time a user hovers over the button or clicks on it. I have this script in every button, how could I not have to add this into each button?

The button looks like this:
ergeg

This is what is in each localscript.

local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local Mouse = LocalPlayer:GetMouse()
local sound = script.Parent.Parent.Parent.Click

local choice = game.ReplicatedStorage.Choice

local ButtonFrame = script.Parent
local Button = ButtonFrame:WaitForChild("Effect")
local Selected = ButtonFrame.SelectedMarker

local OriginalColor = ButtonFrame.BackgroundColor3

local function MakeEffect()
	local Pulse = Instance.new("Frame")
	Pulse.Parent = ButtonFrame
	Pulse.BackgroundColor3 = ButtonFrame.BackgroundColor3
	Pulse.AnchorPoint = Vector2.new(0.5, 0.5)
	Pulse.Position = UDim2.fromScale(0.5, 0.5)
	Pulse.Size = UDim2.new(1, 0, 1, 0)
	Pulse.ZIndex = -1
	Pulse.BorderSizePixel = 0
	Pulse.BackgroundTransparency = 0.5

	local MaxGrow = 0.25
	local Grow = TweenService:Create(Pulse, TweenInfo.new(0.25), {Size = UDim2.fromScale(Pulse.Size.X.Scale+MaxGrow, Pulse.Size.Y.Scale+MaxGrow)})
	local Fade = TweenService:Create(Pulse, TweenInfo.new(0.25, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0.15), {BackgroundTransparency = 1})

	Grow:Play()
	Fade:Play()
	Fade.Completed:Wait()
	Pulse:Destroy()
end

local function EnterMouse()
	sound:Play()
	MakeEffect()
end

local function LeaveMouse()
	-- Something
end

local function ClickMouse()
	choice.Value = "ClickingMode"
	Selected.Visible = true
	sound:Play()
	MakeEffect()
end

Button.MouseEnter:Connect(EnterMouse)
Button.MouseLeave:Connect(LeaveMouse)
Button.MouseButton1Click:Connect(ClickMouse)

You could use for loops.

for _, Button:TextButton in script.Parent:GetChildren() do
    -- then the code here but instead of script.Parent its Button
end

Edit : Also, you dont need TweenService to make them grow and shrink, GUIObjects already have functions called :TweenSize(),:TweenPosition(), and "TweenSizeAndPosition()

1 Like

So would it be something like this?

local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local Mouse = LocalPlayer:GetMouse()
local sound = script.Parent.Parent.Parent.Click

local choice = game.ReplicatedStorage.Choice

local ButtonFrame = script.Parent
local Button = ButtonFrame:WaitForChild("Effect")
local Selected = ButtonFrame.SelectedMarker

local OriginalColor = ButtonFrame.BackgroundColor3

local function MakeEffect()
	local Pulse = Instance.new("Frame")
	Pulse.Parent = ButtonFrame
	Pulse.BackgroundColor3 = ButtonFrame.BackgroundColor3
	Pulse.AnchorPoint = Vector2.new(0.5, 0.5)
	Pulse.Position = UDim2.fromScale(0.5, 0.5)
	Pulse.Size = UDim2.new(1, 0, 1, 0)
	Pulse.ZIndex = -1
	Pulse.BorderSizePixel = 0
	Pulse.BackgroundTransparency = 0.5

	local MaxGrow = 0.25
	local Grow = TweenService:Create(Pulse, TweenInfo.new(0.25), {Size = UDim2.fromScale(Pulse.Size.X.Scale+MaxGrow, Pulse.Size.Y.Scale+MaxGrow)})
	local Fade = TweenService:Create(Pulse, TweenInfo.new(0.25, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0.15), {BackgroundTransparency = 1})

	Grow:Play()
	Fade:Play()
	Fade.Completed:Wait()
	Pulse:Destroy()
end

local function EnterMouse()
	sound:Play()
	MakeEffect()
end

local function LeaveMouse()
	-- Something
end


for _, Button:TextButton in script.Parent:GetChildren() do
	local function ClickMouse()
		choice.Value = "ClickingMode"
		Selected.Visible = true
		sound:Play()
		MakeEffect()
	end
Button.MouseButton1Click:Connect(ClickMouse)
end


Button.MouseEnter:Connect(EnterMouse)
Button.MouseLeave:Connect(LeaveMouse)

Move the MouseEnter and MouseLeave functions into the loop.

for _, Button:TextButton in script.Parent:GetChildren() do
	local function ClickMouse()
		choice.Value = "ClickingMode"
		Selected.Visible = true
		sound:Play()
		MakeEffect()
	end

	Button.MouseButton1Click:Connect(ClickMouse)

	Button.MouseEnter:Connect(EnterMouse)
	Button.MouseLeave:Connect(LeaveMouse)
end

Edit : Sorry for the late reply, I didn’t see the notification.

Make a class for those buttons and then bind the function to those buttons.