Sorry this took so long, but here ya go.
1.) On line 4, we're starting the generic for loop. This is a loop that goes through stuff in a table/array and does stuff with each element. When declaring this loop, you are declaring 2 variables which you can use within the loop. On the wiki, you'll see for i, v in pairs(table) do. In that example, the variables are i and v. "v" is the element, and "i" is the key, or where "v" is in the table/array. In the table t = {"noob", "lol", "hax"}, the element with a key of 2 is the element "lol". So, if we do for i, v in pairs(t) do print(i, v) end, we'll get...
1 noob
2 lol
3 hax
Now, we can actually change the name of the key and element variables that we're using for generic for loops. Let's take a look at your code. The underscore is the key variable. We make it an underscore because we don't really need it. Let's rename "v", the element variable, to "player", just for legibility. Now, we have:
for _, player in pairs(players) do
player.PlayerGui.Status.TextLabel.Text = h.Value
end
2.) I recommend putting local players = game.Players:GetPlayers() in the first line of the function instead of doing players = game.Players:GetChildren()in the beginning of the script. First of all, I'm using GetPlayers(), a function of Players, simply because that function was made for doing just that. GetChildren()would also work. What's important is that you put the line in the function, not outside. If you call GetPlayers/GetChildren outside, you're only calling it when the script begins. When more players come, that table/array isn't going to be updated. However, if you call it whenever you touch the brick, GetPlayers/GetChildren will get all the players every time you touch it, not when the game begins.
tl;dr fixed script
h = script.Parent.Parent.hit
function onTouched(hit)
local players = game.Players:GetPlayers()
for _, player in pairs(players) do
player.PlayerGui.Status.TextLabel.Text = h.Value
end
game.StarterGui.Status.TextLabel.Text = h.Value
end
script.Parent.Touched:connect(onTouched)
2 comments
Help?
Please don't duplicate your question