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

Need help with a shop script please?

Asked by 9 years ago

I have a shop script that uses a crafting system, but in a way my shop is unfair. Let me explain:

EX: Jimmy wants to try to buy a sword, he has 3 mushrooms but not 2 cows, when he presses the button, all the required amount of Jimmy's mushrooms are gone and the transaction is not made.

local items = script.Parent.Frame:GetChildren()

for i = 1, #items do
function buy()
    local item1 = items[i]:FindFirstChild("Item1")
    local item2 = items[i]:FindFirstChild("Item2")
    local item3 = items[i]:FindFirstChild("Item3")
    local item4 = items[i]:FindFirstChild("Item4")
    local item5 = items[i]:FindFirstChild("Item5")
    local Cash = script.Parent.Parent.Parent.Parent.leaderstats:FindFirstChild("Gold")
    if item1 then
        if script.Parent.Parent.Parent.Parent.Materials:FindFirstChild(item1.Value).Value > items   [i].item1Price.Value then
            script.Parent.Parent.Parent.Parent.Materials:FindFirstChild(item1.Value).Value =    script.Parent.Parent.Parent.Parent.Materials:FindFirstChild(item1.Value).Value - items  [i].item1Price.Value
            else return
        end
    end
    if item2 then
        if script.Parent.Parent.Parent.Parent.Materials:FindFirstChild(item2.Value).Value > items   [i].item2Price.Value then
            script.Parent.Parent.Parent.Parent.Materials:FindFirstChild(item2.Value).Value =    script.Parent.Parent.Parent.Parent.Materials:FindFirstChild(item2.Value).Value - items  [i].item2Price.Value
            else return
        end
    end
    if item3 then
        if script.Parent.Parent.Parent.Parent.Materials:FindFirstChild(item3.Value).Value > items   [i].item3Price.Value then
            script.Parent.Parent.Parent.Parent.Materials:FindFirstChild(item3.Value).Value =    script.Parent.Parent.Parent.Parent.Materials:FindFirstChild(item3.Value).Value - items  [i].item3Price.Value
            else return
        end
    end
    if item4 then
        if script.Parent.Parent.Parent.Parent.Materials:FindFirstChild(item4.Value).Value > items   [i].item4Price.Value then
            script.Parent.Parent.Parent.Parent.Materials:FindFirstChild(item4.Value).Value =    script.Parent.Parent.Parent.Parent.Materials:FindFirstChild(item4.Value).Value - items  [i].item4Price.Value
            else return
        end
    end
    if item5 then
        if script.Parent.Parent.Parent.Parent.Materials:FindFirstChild(item5.Value).Value > items   [i].item5Price.Value then
            script.Parent.Parent.Parent.Parent.Materials:FindFirstChild(item5.Value).Value =    script.Parent.Parent.Parent.Parent.Materials:FindFirstChild(item5.Value).Value - items  [i].item5Price.Value
            else return
        end
    end
    if Cash.Value >= items[i].Cost.Value then
        Cash.Value = Cash.Value - items[i].Cost.Value
        local tool1 = game.ReplicatedStorage:FindFirstChild (script.Parent.Folder.Value):FindFirstChild(items[i].ItemName.Value):Clone()
        local tool2 = game.ReplicatedStorage:FindFirstChild (script.Parent.Folder.Value):FindFirstChild(items[i].ItemName.Value):Clone()
        tool1.Parent = script.Parent.Parent.Parent.Parent.Backpack
        tool2.Parent = script.Parent.Parent.Parent.Parent.StarterGear
        print("Purchase successful.")
    end
items[i].BuyButton.MouseButton1Click:connect(buy)
end
end

no errors appear in the output or script analysis

1 answer

Log in to vote
0
Answered by 9 years ago

Combine your conditions into one

Instead of using a sequence of if statements, and them things going horribly wrong when one of them doesn't say yes, evaluate all of your conditions at once using and

if item1 and item2 and item3 and item4 and item5 then
-- Has them all
end
0
but not all weapons have 5 items for crafting. connor12260311 383 — 9y
0
Then make it suitable? Your issue isn't that not all weapons need 5, it's that when you only have some it removes what you have and escapes. My solution fixes that issue. User#6546 35 — 9y
Ad

Answer this question