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

How do i make it so when i press a button it gets a new array from a table?

Asked by 6 years ago

I don't know how to put this into words properly but i want it so when i press x the array gets added by 1 for example...



id = { "apple", "sad" , 202049 } game.Players.LocalPlayer:GetMouse().KeyDown:connect(function(key) if string.lower(key) == "x" then print(id[+1]) end end)

console when i press x

apple

when i press it again

sad

I appreciate your help!

1 answer

Log in to vote
0
Answered by 6 years ago

You need a variable to keep track of the index in the array, like this:

id = { "apple", "sad" , 202049 }
index = 1
game.Players.LocalPlayer:GetMouse().KeyDown:connect(function(key)
    if string.lower(key) == "x" then
        print(id[index])
        index = (index % #id) + 1
    end
end)

So, the first time it will run id[1] and increase index to 2. Then it will run id[2] and increase index to 3. Then it will run id[3] and wrap index to 1 (because 3%3 == 0) so that it can repeat this indefinitely.

0
Thanks so much! T0tallyN0tATr0ll -2 — 6y
1
I'd also recommend looking into UserInputService or ContextAction service, as KeyDown and KeyUp are deprecated. Thundermaker300 554 — 6y
0
It was just an example my actual script uses gui buttons, But thanks for the tip! T0tallyN0tATr0ll -2 — 6y
Ad

Answer this question