Shorten a script

Hello, does anyone have a way to shorten this script?

script.Parent.Parent.BackforNext.MouseButton1Down:Connect(function()
	if script.Parent.Parent.number.Text == "3/3" then
		script.Parent.Parent.CreditsTitle.Text = "2"
		game.SoundService.Click:Play()
		script.Parent.Parent.number.Text = "2/3"
		isDone = false
	end
end)

script.Parent.Parent.BackforNext.MouseButton1Down:Connect(function()
	if script.Parent.Parent.number.Text == "2/3" then
		script.Parent.Parent.CreditsTitle.Text = "1"
		game.SoundService.Click:Play()
		script.Parent.Parent.number.Text = "1/3"
		script.Parent.Parent.BackforNext.Visible = false
		script.Parent.Parent.Next.Position = UDim2.new(0.437, 0,0.603, 0)
	end
end)

I forgot how to make it shorter; it works fine, but I want to make it shorter so I can understand the script because I stopped working on the game, and when I came back, I found out that it was not good enough to read the script and understand how it works, so I need to make it shorter.

2 Likes

its best to use variables (30charrr)

I did this. Is this good? It still works, but Idk.

script.Parent.Parent.BackforNext.MouseButton1Down:Connect(function()
	if script.Parent.Parent.number.Text == "3/3" then
		script.Parent.Parent.number.Text = "2/3"
		script.Parent.Parent.CreditsTitle.Text = "2"
		game.SoundService.Click:Play()
		isDone = false
	elseif script.Parent.Parent.number.Text == "2/3" then
		script.Parent.Parent.number.Text = "1/3"
		script.Parent.Parent.CreditsTitle.Text = "1"
		script.Parent.Parent.BackforNext.Visible = false
		script.Parent.Parent.Next.Position = UDim2.new(0.437, 0,0.603, 0)
		game.SoundService.Click:Play()
	end
end)
1 Like

This isn’t completely efficient because I don’t know how everything looks. It’s just a tiny bit shorter but here:

local parent = script.Parent.Parent -- replace the var's name w whatever u think fits
local BackforNext = parent.BackforNext
BackforNext.MouseButton1Down:Connect(function()
	if parent.number.Text == "3/3" then
		parent.CreditsTitle.Text = "2"
		game.SoundService.Click:Play()
		parent.number.Text = "2/3"
		isDone = false
	end
end)

BackforNext.MouseButton1Down:Connect(function()
	if parent.number.Text == "2/3" then
	        parent.CreditsTitle.Text = "1"
		game.SoundService.Click:Play()
		parent.number.Text = "1/3"
		BackforNext.Visible = false
		parent.Next.Position = UDim2.new(0.437, 0,0.603, 0)
	end
end)

Also, just a recommendation for you next time; make sure to add comments just in case so you understand how certain parts of your code work. it’s a good habit to develop especially if you decide to work with teams in the future and people are reading and trying to understand your code.

1 Like

Okay, Thank you bro for the Information and the code!

1 Like

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