How can I make this script simpler? (Beginner Scripter) Don't judge

Hi, I am trying to learn how to script but I do not know how to use modules and other stuff like that, I’ve hit a roadblock basically.

I don’t really understand that much since I haven’t tried for a few months, are there any tips you guys can give me to make this simpler? Like where to put certain things and how to make it cleaner, maybe how to make it so I don’t have to write the

If number.Text == "Number" or "Number" then
        game.Workspace.Folder.Part.Transparency = 0
                game.Workspace.Folder.Part.CanCollide = true
        end
local players = game:GetService("Players"):GetPlayers()
local button = script.Parent
local number = game.Workspace.Number.BillboardGui.TextLabel
number.Text = "0"


local debounce = false


button.ClickDetector.MouseClick:Connect(function()
        
        if debounce then return end
        debounce = true
        
        number.Text = number.Text +1
        
        wait(0.2)
        debounce = false
end)
button.ClickDetector.MouseClick:Connect(function(player)
        if player.MembershipType == Enum.MembershipType.Premium then
        if debounce then return end
        debounce = true

        number.Text = number.Text +2

        wait(0.2)
                debounce = false
                end
end)

button.ClickDetector.MouseClick:Connect(function()
if number.Text == "100" or "101" then
        game.Workspace.Folder.Part.Transparency = 0
                game.Workspace.Folder.Part.CanCollide = true
        end  
        if number.Text == "200" then
                game.Workspace.Folder.Part1.Transparency = 0
                game.Workspace.Folder.Part1.CanCollide = true
        end
        if number.Text == "300" then
                game.Workspace.Folder.Part2.Transparency = 0
                game.Workspace.Folder.Part2.CanCollide = true
        end
        if number.Text == "400" then
                game.Workspace.Folder.Part3.Transparency = 0
                game.Workspace.Folder.Part3.CanCollide = true
        end
        if number.Text == "500" then
                game.Workspace.Folder.Part4.Transparency = 0
                game.Workspace.Folder.Part4.CanCollide = true
        end
        if number.Text == "600" then
                game.Workspace.Folder.Part5.Transparency = 0
                game.Workspace.Folder.Part5.CanCollide = true
        end
        if number.Text == "700" then
                game.Workspace.Folder.Part6.Transparency = 0
                game.Workspace.Folder.Part6.CanCollide = true
        end
        if number.Text == "800" then
                game.Workspace.Folder.Part7.Transparency = 0
                game.Workspace.Folder.Part7.CanCollide = true
        end
        if number.Text == "900" then
                game.Workspace.Folder.Part8.Transparency = 0
                game.Workspace.Folder.Part8.CanCollide = true
        end
        if number.Text == "1000" then
                game.Workspace.Folder.Part9.Transparency = 0
                game.Workspace.Folder.Part9.CanCollide = true
        end
        if number.Text == "1100" then
                game.Workspace.Folder.Part10.Transparency = 0
                game.Workspace.Folder.Part10.CanCollide = true
        end
                end)```

I’m not really a scripter. But one thing I know is use task.wait instead of just wait. Its faster to my knowledge.

Instead of this you should be doing this:

local Whitelist = {100, 101, 200, 400, 500} -- blah blah blah 
button.ClickDetector.MouseClick:Connect(function()
    if table.find(Whitelist, tonumber(number.Text)) then
        game.Workspace.Folder.Part.Transparency = 0
        game.Workspace.Folder.Part.CanCollide = true
    end
end)
1 Like

This should be the ideal version of your code:

local players = game:GetService("Players"):GetPlayers()
local button = script.Parent
local number = game.Workspace.Number.BillboardGui.TextLabel
number.Text = "0"

local debounce = false

button.ClickDetector.MouseClick:Connect(function(player)
  if debounce then return end
  debounce = true

  if player.MembershipType == Enum.MembershipType.Premium then
    number.Text = number.Text + 2
  else
    number.Text = number.Text + 1
  end

  task.wait(0.2)
  debounce = false
end)

button.ClickDetector.MouseClick:Connect(function()
  local value = tonumber(number.Text)
  if value >= 100 and value <= 1100 then
    local part = game.Workspace.Folder["Part" .. math.floor((value - 1) / 100)]
    if part then
      part.Transparency = 0
      part.CanCollide = true
    end
  end
end)

Here are a few tips that might help you understand and improve your code:

  1. Use comments to explain what each part of your code is doing. This will make it easier for you to understand your own code, and for others to understand it as well. You can add comments by using two hyphens ( -- ) followed by your comment text.
  2. Use indentation to visually organize your code and make it easier to read. This means using spaces or tabs to align blocks of code at the same level.
  3. Use variables to store values that you will use multiple times in your code. This makes it easier to change those values later, and helps you avoid repeating yourself.
  4. Use functions to group related code together and make it easier to reuse. You can define a function by using the function keyword followed by the function name, and then enclosing the code in the function in curly braces ( { and } ).
  5. Use local variables wherever possible to reduce the risk of conflicts with other scripts or modules. Local variables are only visible within the function or script where they are defined, so they are less likely to cause problems.
  6. Use modules to organize your code and make it easier to share with others. Modules are separate scripts that can be imported into other scripts using the require function. This allows you to reuse code without having to copy and paste it into multiple scripts.
3 Likes

Thank you for this! Also, can you explain how this works a bit more to me, I feel like I’d need to use this a lot.

 local value = tonumber(number.Text)
  if value >= 100 and value <= 1100 then
    local part = game.Workspace.Folder["Part" .. math.floor((value - 1) / 100)]

Thank you!

@Dyzody’s algorithm works by checking if the value is greater than or equal to 100 and its less than or equal to 1100 and then indexing the folder by the returned number. Now the formula, normally 100 / 100 would be 1, but since were using math.floor (rounds 1 integer below if the number is a decimal.) But 100 - 1 = 99 and then 99 / 100 would be a decimal number, then math.floor will give us an integer below which is 0, the math.floor and the subtraction with 1 give a 1 off offset, then the reason for dividing for 100 is to get the how many times does it need to be multiplied by 100 to be the number.Text, and your part names are set in this numerical order (part100 / 100, part200 / 100, part300 / 100, etc), which is very convenient to this formula.

Also, a part shows up when it hits 102, 202, 302, etc
Is there any way to fix that

....globals 
function updateNumber(Player)
        if debounce then return end
        if player.MembershipType == Enum.MembershipType.Premium then
         number.Text = tonumber(number.Text) +2
        else 
          number.Text = tonumber(number.Text) +2
        end
        debounce = true
        wait(0.2)
        debounce = false
end
function onClick(player)
     updateNumber(player)
     local CurrentValue = tonumber(number.Value)
     local Scaled = math.max(0,math.floor(CurrentValue/100)-1)
     game.Workspace.Folder["Part"..tostring(Scaled)].Transparency = 0
     game.Workspace.Folder["Part"..tostring(Scaled)].CanCollide = true
end

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