Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is there a more efficient way of going though options in order?

Asked by
DBoi941 57
4 years ago

I am sure there is a more efficient way of doing this I just don't know what it is. What I am trying to have happen is each time the player clicks the button to go through each one in order then start back over.

--// Required //--
local plot = script.Parent.Parent.Parent.BasePartLand
local numMaterial = 1
local numColor = 1

--// Buttons //--
local material = script.Parent.Material.SurfaceGui.TextButton
local color = script.Parent.Material.SurfaceGui.TextButton

material.MouseButton1Click:connect(function()
    if numMaterial == 1 then
        plot.Material = Enum.Material.Cobblestone
        numMaterial = numMaterial + 1
    elseif numMaterial == 2 then
        plot.Material = Enum.Material.Brick
        numMaterial = numMaterial + 1
    elseif numMaterial == 3 then
        plot.Material = Enum.Material.DiamondPlate
        numMaterial = numMaterial + 1
    elseif numMaterial == 4 then
        plot.Material = Enum.Material.Foil
        numMaterial = numMaterial + 1
    elseif numMaterial == 5 then
        plot.Material = Enum.Material.Granite
        numMaterial = numMaterial + 1
    elseif numMaterial == 6 then
        plot.Material = Enum.Material.Grass
        numMaterial = 1
    end
end)

1 answer

Log in to vote
1
Answered by
karlo_tr10 1233 Moderation Voter
4 years ago

First of all, you should use :Connect instead of :connect because it's deprecated. Here is an improved code:

--// Required //--
local plot = script.Parent.Parent.Parent.BasePartLand
local numMaterial = 1
local numColor = 1

local materials = {
    ["1"] = Enum.Material.Cobblestone;
    ["2"] = Enum.Material.Brick;
    ["3"] = Enum.Material.DiamondPlate;
    ["4"] = Enum.Material.Foil;
    ["5"] = Enum.Material.Granite;
    ["6"] = Enum.Material.Grass;
}

--// Buttons //--
local material = script.Parent.Material.SurfaceGui.TextButton
local color = script.Parent.Material.SurfaceGui.TextButton

material.MouseButton1Click:Connect(function()  
    plot.Material = materials[tostring(numMaterial)]
    if numMaterial == 6 then
        numMaterial = 1
    else
        numMaterial = numMaterial + 1
    end   
end)
0
Thank you very much. DBoi941 57 — 4y
0
No problem. karlo_tr10 1233 — 4y
Ad

Answer this question