How can I make this code much shorter?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Hello developers! I am currently working on a game with a level select that lets you select from a wide variety of levels.
  2. What is the issue? Include screenshots / videos if possible!
    I want to keep the script short and simple, and not have to copy and paste every .MouseEnter or .MouseLeave or .MouseButton1Click function for each button.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’m not sure, but maybe having a table with all the Ids of every level. And loop through that?

local TeleportService = game:GetService("TeleportService")

local SFX = script:WaitForChild("SFX")

local Click_sound = script:WaitForChild("ClickSound")

local Place_ID1 = 11453544275 -- Tropical Coast

local Place_ID2 = 10665512129 -- Wave Square

local Place_ID3 = 10669943980 -- Adrenaline Highway

local Place_ID4 = 10677378022 -- Snow Cap

local Place_ID5 = 10684140458 -- Ruined Base

local Place_ID6 = 10696025368 -- Seaside Coast

local Place_ID7 = 10892018924 -- Seaside Coast (ACT 2)

local Place_ID8 = 10892025301 -- Checker Hill

local Place_ID9 = 10892037059 -- Find your Flame

local Place_ID10 = 15264722817 -- Find your Flame (ACT 2)



Trial.MouseButton1Click:Connect(function()
	
	if Confirmation.Visible == true then -- Stops from overlapping.
		Confirmation.Visible = false
	end
	
	SFX:Play()
	
	BaseGame.Visible = false
	NoobStory.Visible = false
	TheFinalRise.Visible = false
	
	Shade.Visible = false
	Shade2.Visible = false
	Shade3.Visible = false
	
	Story_Select.Visible = false
	Story_SelectShadow.Visible = false
	
	Guide.Visible = false
	GuideShadow.Visible = false
	
	
	if TrialFrame.Visible == false then -- If LevelselectFrame is not visible.
		TrialFrame.Visible = true -- Make it visible.
	else -- Or else...
		
		TrialFrame.Visible = false
		
		
		BaseGame.Visible = true
		NoobStory.Visible = true
		TheFinalRise.Visible = true
		
		Shade.Visible = true
		Shade2.Visible = true
		Shade3.Visible = true
		
		
		Story_Select.Visible = true
		Story_SelectShadow.Visible = true
		
		Guide.Visible = false
		GuideShadow.Visible = false
	end
end)


Stage01.MouseEnter:Connect(function()
	Click_sound:Play()
	Stage01.BackgroundColor3 = Color3.fromRGB(86, 316, 355)
end)



Stage01.MouseLeave:Connect(function()
	Stage01.BackgroundColor3 = Color3.fromRGB(66, 116, 255)
end)



Stage02.MouseEnter:Connect(function()
	Click_sound:Play()
	Stage02.BackgroundColor3 = Color3.fromRGB(86, 316, 355)
end)



Stage02.MouseLeave:Connect(function()
	Stage02.BackgroundColor3 = Color3.fromRGB(66, 116, 255)
end)



Stage03.MouseEnter:Connect(function()
	Click_sound:Play()
	Stage03.BackgroundColor3 = Color3.fromRGB(86, 316, 355)
end)



Stage03.MouseLeave:Connect(function()
	Stage03.BackgroundColor3 = Color3.fromRGB(66, 116, 255)
end)



Stage04.MouseEnter:Connect(function()
	Click_sound:Play()
	Stage04.BackgroundColor3 = Color3.fromRGB(86, 316, 355)
end)



Stage04.MouseLeave:Connect(function()
	Stage04.BackgroundColor3 = Color3.fromRGB(66, 116, 255)
end)



Stage05.MouseEnter:Connect(function()
	Click_sound:Play()
	Stage05.BackgroundColor3 = Color3.fromRGB(86, 316, 355)
end)



Stage05.MouseLeave:Connect(function()
	Stage05.BackgroundColor3 = Color3.fromRGB(66, 116, 255)
end)



Stage06.MouseEnter:Connect(function()
	Click_sound:Play()
	Stage06.BackgroundColor3 = Color3.fromRGB(86, 316, 355)
end)



Stage06.MouseLeave:Connect(function()
	Stage06.BackgroundColor3 = Color3.fromRGB(66, 116, 255)
end)



Stage07.MouseEnter:Connect(function()
	Click_sound:Play()
	Stage07.BackgroundColor3 = Color3.fromRGB(86, 316, 355)
end)



Stage07.MouseLeave:Connect(function()
	Stage07.BackgroundColor3 = Color3.fromRGB(66, 116, 255)
end)



Stage08.MouseEnter:Connect(function()
	Click_sound:Play()
	Stage08.BackgroundColor3 = Color3.fromRGB(86, 316, 355)
end)



Stage08.MouseLeave:Connect(function()
	Stage08.BackgroundColor3 = Color3.fromRGB(66, 116, 255)
end)



Stage09.MouseEnter:Connect(function()
	Click_sound:Play()
	Stage09.BackgroundColor3 = Color3.fromRGB(86, 316, 355)
end)



Stage09.MouseLeave:Connect(function()
	Stage09.BackgroundColor3 = Color3.fromRGB(66, 116, 255)
end)



Stage10.MouseEnter:Connect(function()
	Click_sound:Play()
	Stage10.BackgroundColor3 = Color3.fromRGB(86, 316, 355)
end)



Stage10.MouseLeave:Connect(function()
	Stage10.BackgroundColor3 = Color3.fromRGB(66, 116, 255)
end)



Stage01.MouseButton1Click:Connect(function()
	SFX:Play()
	TeleportService:Teleport(Place_ID1, player) -- Teleports the player.
end)



Stage02.MouseButton1Click:Connect(function()
	SFX:Play()
	TeleportService:Teleport(Place_ID2, player) -- Teleports the player.
end)



Stage03.MouseButton1Click:Connect(function()
	SFX:Play()
	TeleportService:Teleport(Place_ID3, player) -- Teleports the player.
end)



Stage04.MouseButton1Click:Connect(function()
	SFX:Play()
	TeleportService:Teleport(Place_ID4, player) -- Teleports the player.
end)



Stage05.MouseButton1Click:Connect(function()
	SFX:Play()
	TeleportService:Teleport(Place_ID5, player) -- Teleports the player.
end)



Stage06.MouseButton1Click:Connect(function()
	SFX:Play()
	TeleportService:Teleport(Place_ID6, player) -- Teleports the player.
end)



Stage07.MouseButton1Click:Connect(function()
	SFX:Play()
	TeleportService:Teleport(Place_ID7, player) -- Teleports the player.
end)



Stage08.MouseButton1Click:Connect(function()
	SFX:Play()
	TeleportService:Teleport(Place_ID8, player) -- Teleports the player.
end)



Stage09.MouseButton1Click:Connect(function()
	SFX:Play()
	TeleportService:Teleport(Place_ID9, player) -- Teleports the player.
end)



Stage10.MouseButton1Click:Connect(function()
	SFX:Play()
	TeleportService:Teleport(Place_ID10, player) -- Teleports the player.
end)
1 Like

A simple table would fix that:

local TeleportService = game:GetService("TeleportService")
local SFX = script:WaitForChild("SFX")
local Click_sound = script:WaitForChild("ClickSound")

local levels = {
    {Button = Stage01, PlaceID = 11453544275},
    {Button = Stage02, PlaceID = 10665512129},
    {Button = Stage03, PlaceID = 10669943980},
    {Button = Stage04, PlaceID = 10677378022},
    {Button = Stage05, PlaceID = 10684140458},
    {Button = Stage06, PlaceID = 10696025368},
    {Button = Stage07, PlaceID = 10892018924},
    {Button = Stage08, PlaceID = 10892025301},
    {Button = Stage09, PlaceID = 10892037059},
    {Button = Stage10, PlaceID = 15264722817}
}

local function setupButton(button, placeID)
    button.MouseEnter:Connect(function()
        Click_sound:Play()
        button.BackgroundColor3 = Color3.fromRGB(86, 316, 355)
    end)

    button.MouseLeave:Connect(function()
        button.BackgroundColor3 = Color3.fromRGB(66, 116, 255)
    end)

    button.MouseButton1Click:Connect(function()
        SFX:Play()
        TeleportService:Teleport(placeID, game.Players.LocalPlayer)
    end)
end

for _, level in ipairs(levels) do
    setupButton(level.Button, level.PlaceID)
end

Trial.MouseButton1Click:Connect(function()
    if Confirmation.Visible then
        Confirmation.Visible = false
    end

    SFX:Play()

    BaseGame.Visible = false
    NoobStory.Visible = false
    TheFinalRise.Visible = false
    Shade.Visible = false
    Shade2.Visible = false
    Shade3.Visible = false
    Story_Select.Visible = false
    Story_SelectShadow.Visible = false
    Guide.Visible = false
    GuideShadow.Visible = false

    if not TrialFrame.Visible then
        TrialFrame.Visible = true
    else
        TrialFrame.Visible = false
        BaseGame.Visible = true
        NoobStory.Visible = true
        TheFinalRise.Visible = true
        Shade.Visible = true
        Shade2.Visible = true
        Shade3.Visible = true
        Story_Select.Visible = true
        Story_SelectShadow.Visible = true
        Guide.Visible = false
        GuideShadow.Visible = false
    end
end)
3 Likes

Shouldn’t you be constantly looping through the buttons though?

No, you only need to connect the buttons to their events once.

1 Like

It works perfectly, thank you for your explanation. This is shorter than before.

local Player = game.Players.LocalPlayer

local PlayerGui = Player.PlayerGui

local CampaignInterface = PlayerGui.CampaignInterface

local Trial = CampaignInterface.Trial
local theStages = Trial.theStages

local Stage01 = theStages.Stage01
local Stage02 = theStages.Stage02
local Stage03 = theStages.Stage03
local Stage04 = theStages.Stage04
local Stage05 = theStages.Stage05
local Stage06 = theStages.Stage06
local Stage07 = theStages.Stage07
local Stage08 = theStages.Stage08
local Stage09 = theStages.Stage09

local Data = {
	{Button = Stage01, PlaceId = 11453544275},
	{Button = Stage02, PlaceId = 10665512129},
	{Button = Stage03, PlaceId = 10669943980},
	{Button = Stage04, PlaceId = 10677378022},
	{Button = Stage05, PlaceId = 10684140458},
	{Button = Stage06, PlaceId = 10696025368},
	{Button = Stage07, PlaceId = 10892018924},
	{Button = Stage08, PlaceId = 10892025301},
	{Button = Stage09, PlaceId = 10892037059},
}

local SoundService = game:GetService("SoundService")
local InterfaceSounds = SoundService:FindFirstChild("InterfaceSounds")
local HoverSound = InterfaceSounds:WaitForChild("HoverSound")
local ButtonSound = InterfaceSounds:WaitForChild("ButtonSound")

local TweenService = game:GetService("TweenService")
local TeleportService = game:GetService("TeleportService")

for i, stage in ipairs(Data) do
	local HighlightLayer = TweenService:Create(
		stage.Button.Layer,
		TweenInfo.new(0.5),
		{BackgroundTransparency = 0.5}
	)
	
	local UnhighlightLayer = TweenService:Create(
		stage.Button.Layer,
		TweenInfo.new(0.5),
		{BackgroundTransparency = 1}
	)
	
	stage.Button.MouseEnter:Connect(function()
		HoverSound:Play()
		HighlightLayer:Play()
	end)
	
	stage.Button.MouseLeave:Connect(function()
		UnhighlightLayer:Play()
	end)
	
	stage.Button.MouseButton1Click:Connect(function()
		ButtonSound:Play()
		TeleportService:Teleport(stage.PlaceId, Player)
	end)
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.