Is there a way i can shorten between line 12 and 19

Hello, so basically I wanted to shorten a small code between line 12 and 19 because I think having them being duplicated multiple times is pretty unnecessary. Is there a way to shorten it?

function dooropen.Scattered(number)
	local code = game:GetService("ReplicatedStorage"):WaitForChild("RoomCodes"):FindFirstChild("Room"..number)
	local randomenabled = game.Workspace.CodeInput.CodeGivers.Scattered["CodeGiver_"..number].Configuration.Random
	local total = game.Workspace.CodeInput.CodeGivers.Scattered["CodeGiver_"..number].Configuration.Total
	local scattered = game.Workspace.CodeInput.CodeGivers.Scattered["CodeGiver_"..number].Configuration.ScatteredNumbers
	local giver = game.Workspace.CodeInput.CodeGivers.Scattered["CodeGiver_"..number]

	if randomenabled.Value == true then
		for i = 1, total.Value do
			task.wait()
			scattered[i].Value = math.random(0, 9)
			code.Value = scattered[1].Value..scattered[2].Value..scattered[3].Value..scattered[4].Value..scattered[5].Value..scattered[6].Value
			wait(0.5)
			giver["Scattered"..i].SurfaceGui.SIGN.Text = i..") "..tostring(scattered[i].Value)
		end
	else
		for i = 1, total.Value do
			task.wait()
			code.Value = scattered[1].Value..scattered[2].Value..scattered[3].Value..scattered[4].Value..scattered[5].Value..scattered[6].Value
			giver["Scattered"..i].SurfaceGui.SIGN.Text = i..") "..tostring(scattered[i].Value)
		end
	end
end

There are lots of way to do this

  1. Loop and put the common stuff inside the loop and then do the if statement.
  2. Another way is to use a function example

Before:

if 2*2 == 4 then
print("hello")
print(2*2)
else

print("hello")
print(2*2)
end

After:

function Namemeanything()

print("hello")
print(2*2)
end

if 2*2 == 4 then
Namemeanything()
else

Namemeanything()
end
1 Like

Pretty sure this is the wrong channel. You should be posting stuff like this in Code Review. Not scripting support.

1 Like

Not really fully sure exactly what you meant, but would this work to have shorter code?

for i = 1, total.Value do
if randomenabled.Value then
scattered[i].Value = math.random(0, 9)
end
code.Value = scattered[1].Value…scattered[2].Value…scattered[3].Value…scattered[4].Value…scattered[5].Value…scattered[6].Value
task.wait(0.5)
giver[“Scattered”…i].SurfaceGui.SIGN.Text = i…") "…tostring(scattered[i].Value)
task.wait()
end

Thank you, this might work. Just one more thing, is there any more tips to shorten codes without using a lot of if/elseif statements?

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