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

I'm trying to understand a script, but I don't get what this means. What is table.foreach for?

Asked by 4 years ago
        table.foreach(Humanoid:GetPlayingAnimationTracks(),function(_,v) v:Stop() end)
            if Character:FindFirstChild("Animate") then Character:FindFirstChild("Animate"):Destroy()
0
See https://developer.roblox.com/en-us/api-reference/lua-docs/table, "Iterates over the provided table, passing the key and value of each iteration over to the provided function." moo1210 587 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

table.foreach iterates over all of the values in a table which is the first argument and then executes the function which is the second argument. For example, if you have a table like this

local Letters = {
    "A",
    "B",
    "C",
    "D"
}

and you want to print every value in the table to console, you can use do

table.foreach(Letters, print)

This will go over every element in the table Letters and execute the function print with the arguments index and value. The output of this script will be

1 A
2 B
3 C
4 D

In the script which you provided, every animation that a humanoid is currently printing will execute this function

function(_, v)
    v:Stop()
end

Since v represents an animation which the humanoid is playing, every animation the humanoid is playing will be stopped.

Ad

Answer this question