Anyway this code can be shortened? my brain isnt working rn

What is this?

  • A random gadget (tool) is selected by a chance system
  • A UI shows the player what they got
--> COMMON: 60% // UNCOMMON: 20% // RARE: 10% // LEGENDARY: 7% // GODLY: 3% <--
	local Chance = math.random(1, 100)
	local Gadget
	if Chance <= 20 then -- 20%
		Gadget = "Fire Ball"
	elseif Chance >= 21 and Chance <= 40 then -- 20%
		Gadget = "Smoke Path"
	elseif Chance >= 41 and Chance <= 60 then -- 20%
		Gadget = "Common3"
	elseif Chance >= 61 and Chance <= 70 then -- 10%
		Gadget = "Sign"
	elseif Chance >= 71 and Chance <= 80 then -- 10%
		Gadget = "Dance Potion"
	elseif Chance >= 81 and Chance <= 85 then -- 5%
		Gadget = "Shield"
	elseif Chance >= 86 and Chance <= 90 then -- 5%
		Gadget = "Rare2"
	elseif Chance >= 91 and Chance <= 97 then -- 7%
		Gadget = "Flare Gun"
	elseif Chance >= 98 and Chance <= 100 then -- 3%
		Gadget = "Scanner"
	end
	RoleSelectorFrame.Role.Text = "Fire Ball"
	RoleSelectorFrame.Role.BackgroundColor3 = Color3.new(0.380392, 0.380392, 0.380392)
	RoleSelectorFrame.ClickSound:Play()
	wait(.1)
	RoleSelectorFrame.Role.Text = "Shield"
	RoleSelectorFrame.Role.BackgroundColor3 = Color3.new(0, 1, 0)
	RoleSelectorFrame.ClickSound:Play()
	wait(.1)
	RoleSelectorFrame.Role.Text = "Sign"
	RoleSelectorFrame.Role.BackgroundColor3 = Color3.new(0, 0.666667, 1)
	RoleSelectorFrame.ClickSound:Play()
	wait(.1)
	RoleSelectorFrame.Role.Text = "Scanner"
	RoleSelectorFrame.Role.BackgroundColor3 = Color3.new(0.666667, 0, 1)
	RoleSelectorFrame.ClickSound:Play()
	wait(.1)
	RoleSelectorFrame.Role.Text = "Smoke Path"
	RoleSelectorFrame.Role.BackgroundColor3 = Color3.new(0.380392, 0.380392, 0.380392)
	RoleSelectorFrame.ClickSound:Play()
	wait(.1)
	RoleSelectorFrame.Role.Text = "Flare Gun"
	RoleSelectorFrame.Role.BackgroundColor3 = Color3.new(1, 0, 0)
	RoleSelectorFrame.ClickSound:Play()
	wait(.1)
	RoleSelectorFrame.Role.Text = "Dance Potion"
	RoleSelectorFrame.Role.BackgroundColor3 = Color3.new(0, 0.666667, 1)
	RoleSelectorFrame.ClickSound:Play()
	wait(.1)
	RoleSelectorFrame.Role.Text = "COMMON3"
	RoleSelectorFrame.Role.BackgroundColor3 = Color3.new(0.380392, 0.380392, 0.380392)
	RoleSelectorFrame.ClickSound:Play()
	wait(.1)
	RoleSelectorFrame.Role.Text = "RARE2"
	RoleSelectorFrame.Role.BackgroundColor3 = Color3.new(0, 1, 0)
	RoleSelectorFrame.ClickSound:Play()
	wait(.1)
	RoleSelectorFrame.Role.Text = "Fire Ball"
	RoleSelectorFrame.Role.BackgroundColor3 = Color3.new(0.380392, 0.380392, 0.380392)
	RoleSelectorFrame.ClickSound:Play()
	if Gadget ~= "Fire Ball" then
		wait(.1)
		RoleSelectorFrame.Role.Text = "Shield"
		RoleSelectorFrame.Role.BackgroundColor3 = Color3.new(0, 1, 0)
		RoleSelectorFrame.ClickSound:Play()
	end
	if Gadget ~= "Shield" and Gadget ~= "Fire Ball" then
		wait(.1)
		RoleSelectorFrame.Role.Text = "Sign"
		RoleSelectorFrame.Role.BackgroundColor3 = Color3.new(0, 0.666667, 1)
		RoleSelectorFrame.ClickSound:Play()
	if Gadget ~= "Sign" and Gadget ~= "Shield" and Gadget ~= "Fire Ball" then
		wait(.1)
		RoleSelectorFrame.Role.Text = "Scanner"
		RoleSelectorFrame.Role.BackgroundColor3 = Color3.new(0.666667, 0, 1)
		RoleSelectorFrame.ClickSound:Play()
	end
	if Gadget ~= "Scanner" and Gadget ~= "Sign" and Gadget ~= "Shield" and Gadget ~= "Fire Ball" then
		wait(.1)
		RoleSelectorFrame.Role.Text = "Smoke Path"
		RoleSelectorFrame.Role.BackgroundColor3 = Color3.new(0.380392, 0.380392, 0.380392)
		RoleSelectorFrame.ClickSound:Play()
	end
	if Gadget ~= "Smoke Path" and Gadget ~= "Scanner" and Gadget ~= "Sign" and Gadget ~= "Shield" and Gadget ~= "Fire Ball" then
		wait(.1)
		RoleSelectorFrame.Role.Text = "Flare Gun"
		RoleSelectorFrame.Role.BackgroundColor3 = Color3.new(1, 0, 0)
		RoleSelectorFrame.ClickSound:Play()
	end
	if Gadget ~= "Flare Gun" and Gadget ~= "Smoke Path" and Gadget ~= "Scanner" and Gadget ~= "Sign" and Gadget ~= "Shield" and Gadget ~= "Fire Ball" then
		wait(.1)
		RoleSelectorFrame.Role.Text = "Dance Potion"
		RoleSelectorFrame.Role.BackgroundColor3 = Color3.new(0, 0.666667, 1)
		RoleSelectorFrame.ClickSound:Play()
	end
	if Gadget ~= "Dance Potion" and Gadget ~= "Flare Gun" and Gadget ~= "Smoke Path" and Gadget ~= "Scanner" and Gadget ~= "Sign" and Gadget ~= "Shield" and Gadget ~= "Fire Ball" then
		wait(.1)
		RoleSelectorFrame.Role.Text = "COMMON3"
		RoleSelectorFrame.Role.BackgroundColor3 = Color3.new(0.380392, 0.380392, 0.380392)
		RoleSelectorFrame.ClickSound:Play()
	end
	if Gadget ~= "Common3" and Gadget ~= "Dance Potion" and Gadget ~= "Flare Gun" and Gadget ~= "Smoke Path" and Gadget ~= "Scanner" and Gadget ~= "Sign" and Gadget ~= "Shield" and Gadget ~= "Fire Ball" then return end
		wait(.1)
		RoleSelectorFrame.Role.Text = "RARE2"
		RoleSelectorFrame.Role.BackgroundColor3 = Color3.new(0, 1, 0)
		RoleSelectorFrame.ClickSound:Play()
	end

that alot of if and elseif
i think you can use table to fix this
like this

local Chance = TableName[math.random(1, #TableName)]

to make certain stuff rarer you can do it like this

Tablename = {a ,a, a, a, a, a, a, a, b, b, b, c}

i dont know if you can store more than two same value in one table

local Chance = math.random(1, 100)
local Gadget

-- this array must be sorted in a decent order relative to the Chance
local Gadgets = {
    {Name = "Fire Ball", Chance = 20, Color = Color3.new(0.380392, 0.380392, 0.380392)},
    {Name = "Smoke Path", Chance = 20, Color = Color3.new(0.380392, 0.380392, 0.380392)},
    {Name = "Common3", Chance = 20, Color = Color3.new(0.380392, 0.380392, 0.380392)},
    {Name = "Sign", Chance = 10, Color = Color3.new(0, 0.666667, 1)},
    {Name = "Dance Potion", Chance = 10, Color = Color3.new(0, 0.666667, 1)},
    {Name = "Flare Gun", Chance = 7, Color = Color3.new(1, 0, 0)},
    {Name = "Shield", Chance = 5, Color = Color3.new(0, 1, 0)},
    {Name = "Rare2", Chance = 5, Color = Color3.new(0, 1, 0)},
    {Name = "Scanner", Chance = 3, Color = Color3.new(0.666667, 0, 1)},
}

local GadgetChance = 0
for _, g in ipairs(Gadgets) do
    GadgetChance += g.Chance
    if Chance <= GadgetChance then
        Gadget = g.Name
        break
    end
end

for _, g in ipairs(Gadgets) do
    task.wait(.1)
    RoleSelectorFrame.Role.Text = g.Name
    RoleSelectorFrame.Role.BackgroundColor3 = g.Color
    RoleSelectorFrame.ClickSound:Play()
end

task.wait(.1)
local ns = {1, 7, 4, 9, 2, 6, 5, 3, 8}
for n = 1, #ns-1 do
    if Gadget ~= Gadgets[ns[n]].Name then
        RoleSelectorFrame.Role.Text = Gadgets[ns[n+1]].Name
        RoleSelectorFrame.Role.BackgroundColor3 = Gadgets[ns[n+1]].Color
        RoleSelectorFrame.ClickSound:Play()
        break
    end
end
2 Likes