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

[Not Solved] Return/If + Shortening Script?

Asked by
iFlusters 355 Moderation Voter
8 years ago

Okay, so this may seem like a big script, but it's rather simple to understand. Basically the function alone changes the transparency of an ImageLabel.

Firstly The problem I am having is with the returncode. I am using return so that it won't play the other transparencies in the if statements, otherwise it won't work. I'm pretty sure there is a way around this but I will also be having 6 cards in the 1 function, as you can see it gets really long, plus the script won't continue after the 1st set of if's in Card1

Secondly As you can see this script is getting rather long, especially as I will be adding 6 other 'Cardx's which is why I am asking if there is a way to shorten it. Here's the code:

function openChest(Player)
    if not debounce then
        debounce = true
            rotate(Lid, -Increment)
        end
    debounce = false

    Item = chooseItem():Clone()
    local chance = getChance(Item)

    local ChestGui = Player.PlayerGui:WaitForChild("ChestGui")
    local Card1 = ChestGui.Card1

        if chance <= 10 then
        Card1.Image = "http://www.roblox.com/asset/?id=" ..GoldCard 
        Card1.ImageTransparency = 0.8
        wait()
        Card1.ImageTransparency = 0.6
        wait()
        Card1.ImageTransparency = 0.4
        wait()
        Card1.ImageTransparency = 0.2
        wait()
        Card1.ImageTransparency = 0
        Item.Parent = Player.Backpack
    return;
end
        if chance <= 50 then
        Card1.Image = "http://www.roblox.com/asset/?id=" ..SilverCard
        Card1.ImageTransparency = 0.8
        wait()
        Card1.ImageTransparency = 0.6
        wait()
        Card1.ImageTransparency = 0.4
        wait()
        Card1.ImageTransparency = 0.2
        wait()
        Card1.ImageTransparency = 0
        Item.Parent = Player.Backpack
    return;
end
        if chance <= 100 then
        Card1.Image = "http://www.roblox.com/asset/?id=" ..BronzeCard
        Card1.ImageTransparency = 0.8
        wait()
        Card1.ImageTransparency = 0.6
        wait()
        Card1.ImageTransparency = 0.4
        wait()
        Card1.ImageTransparency = 0.2
        wait()
        Card1.ImageTransparency = 0
        Item.Parent = Player.Backpack
    return;
end

        Item = chooseItem():Clone()
        local chance = getChance(Item)

        local ChestGui = Player.PlayerGui:WaitForChild("ChestGui")
        local Card2 = ChestGui.Card2

        if chance <= 10 then
        Card2.Image = "http://www.roblox.com/asset/?id=" ..GoldCard 
        Card2.ImageTransparency = 0.8
        wait()
        Card2.ImageTransparency = 0.6
        wait()
        Card2.ImageTransparency = 0.4
        wait()
        Card2.ImageTransparency = 0.2
        wait()
        Card2.ImageTransparency = 0
        Item.Parent = Player.Backpack
    return;
end
        if chance <= 50 then
        Card2.Image = "http://www.roblox.com/asset/?id=" ..SilverCard
        Card2.ImageTransparency = 0.8
        wait()
        Card2.ImageTransparency = 0.6
        wait()
        Card2.ImageTransparency = 0.4
        wait()
        Card2.ImageTransparency = 0.2
        wait()
        Card2.ImageTransparency = 0
        Item.Parent = Player.Backpack
    return;
end
        if chance <= 100 then
        Card2.Image = "http://www.roblox.com/asset/?id=" ..BronzeCard
        Card2.ImageTransparency = 0.8
        wait()
        Card2.ImageTransparency = 0.6
        wait()
        Card2.ImageTransparency = 0.4
        wait()
        Card2.ImageTransparency = 0.2
        wait()
        Card2.ImageTransparency = 0
        Item.Parent = Player.Backpack
    return;
end
    Chest.Lid.ClickSensor:Destroy()
end

All help is much appreciated.

1 answer

Log in to vote
1
Answered by 8 years ago

You can use elseifs to eliminate the returns. Like so:

if condition then
    --...
elseif condition2 then
    --...
end

For your second question, look into how tables work in Lua. If you make a table of cards and loop through it, you can shorten your code for all 6 cards.

0
You can chain more elseifs indefinitely. So yes, you can use 3 ifs. iconmaster 301 — 8y
0
My bad, I'm not too good with loops though, which is why I'm asking for help on it. iFlusters 355 — 8y
Ad

Answer this question