Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Script gives two of the same thing, not one?

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)

3 comments

0

From a quick look, I can't tell you what is up, but I can tell you that your matching logic is flawed.

0

yeah i noticed

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.

1 answer

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.

2 comments

0

thanks

0

i'll try and figure out why i'm getting double moves

This discussion is preserved as it was. New answers, comments, and votes are closed.