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

Script gives two of the same thing, not one?

Asked by 8 years ago

My previous question was why a script that was supposed to give tools wasn't working. So I managed to create a better script, which uses loops and tables. It works! But wait, when I tested it out, with Squirtle, it gave two of the same move. Charmander works fine. The move that gets cloned twice is Tackle. I don't know what's wrong.

moves = {"Ember", "Tackle", "Bubble"}
charmander = {"Ember", "Tackle"}
squirtle = {"Bubble", "Tackle"}
movestorage = workspace.Moves
plr = game.Players.LocalPlayer

script.Parent.Charmander.MouseButton1Click:connect(function()
    for i = 1,#charmander do
        if moves[i] == charmander[i] then
            local move = movestorage:FindFirstChild(moves[i]):Clone()
            move.Parent = plr.Backpack
        end
    end
end)
-- MY PROBLEM IS HERE --
script.Parent.Squirtle.MouseButton1Click:connect(function()
    for i = 1,#squirtle do
        if moves[i] == squirtle[i] then
            local move = movestorage:FindFirstChild(moves[i]):Clone()
            move.Parent = plr.Backpack
        end
    end
end)
0
From a quick look, I can't tell you what is up, but I can tell you that your matching logic is flawed. PlsNoDiscrimination 0 — 8y
0
yeah i noticed Operation_Meme 890 — 8y
0
Try using a debounce in the functions if you know how to use them it's really useful and also prevents the function from running more then once with a cool down. KingLoneCat 2642 — 8y

1 answer

Log in to vote
0
Answered by 8 years ago

I don't have a reason for why you're getting double, but I do have a reason you're not getting Bubble.

script.Parent.Squirtle.MouseButton1Click:connect(function()
    for i = 1,#squirtle do
        if moves[i] == squirtle[i] then
            local move = movestorage:FindFirstChild(moves[i]):Clone()
            move.Parent = plr.Backpack
        end
    end
end)

moves[i] will never be squirtle[i] when i is 1, nor even when it is 3, and therefore you just can't get Bubble. If you want to check if something is in a table, you'll have to iterate over the whole table in two parts. Not ideal.

The trick is that in your case, you don't need to check. Just use

script.Parent.Squirtle.MouseButton1Click:connect(function()
    for i = 1,#squirtle do
            local move = movestorage:FindFirstChild(squirtle[i]):Clone()
            move.Parent = plr.Backpack
    end
end)

Instead in your Squirtle loop. Likewise for the Charmander loop.

0
thanks Operation_Meme 890 — 8y
0
i'll try and figure out why i'm getting double moves Operation_Meme 890 — 8y
Ad

Answer this question