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

How come this script won't work without the player resetting?

Asked by 9 years ago

Please do not repost your question after it has been closed.

I'm trying to make a Text Label's text the value of an IntValue.

players = game.Players:GetChildren()
h = script.Parent.Parent.hit--intValue
function onTouched(hit)
    for _,v in pairs(players) do
        do players.PlayerGui.Status.TextLabel.Text = h.Value
    end
    game.StarterGui.Status.TextLabel.Text = h.Value
end

script.Parent.Touched:connect(onTouched)


Here is the hierarchy http://imgur.com/V9shihA

0
Help? fattyfateric1 0 — 9y
0
Please don't duplicate your question FearMeIAmLag 1161 — 9y

2 answers

Log in to vote
0
Answered by 9 years ago

I believe the only problem with this is that there is no wait in the for loop and that you have that extra do on line 5 which should not be there. Other than that this script looks like it should work fine.

EDIT: I would change line 5 to this:

v.PlayerGui.Status.TextLabel.Text = h.Value
Ad
Log in to vote
0
Answered by
funyun 958 Moderation Voter
9 years ago

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 --Take "do" off of this line and put "player" instead of "players" in your script.
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--intValue

function onTouched(hit)
    local players = game.Players:GetPlayers() --Call GetPlayers/GetChildren INSIDE the function

    for _, player in pairs(players) do
        player.PlayerGui.Status.TextLabel.Text = h.Value --Take "do" out of this line, change "players" to "player"
    end

    game.StarterGui.Status.TextLabel.Text = h.Value
end

script.Parent.Touched:connect(onTouched)

Answer this question