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

For loop with table?

--Problem Solved Note that in-game the table is filled with real player names. Unfixed V

tablename = {"Player1", 100, "Player2", 200}

for i = 1,#tablename, 2 do
	game.Players[tablename[i]].leaderstats.money.Value = tablename[i+1]
end

Fixed V

tablename = {"Player1", 100, "Player2", 200}

for i = 1,#tablename, 2 do
        if game.Players[_G.realstats[i]].leaderstats.money then -- don't break yourself mr.script
	game.Players[tablename[i]].leaderstats.money.Value = tablename[i+1]
        end
end

7 comments

0

what if theres no Player1 or Roblox ingame? e.e

0

this isn't the entire script, the table is filled with real players

0

The contents of the table isn't the problem.

0

print(#_G.realstats) immediately before the loop -- what do you get? Most likely, you're not filling the table correctly

0

http://i.imgur.com/LPj5g45.png As you can see, the table is set correctly according to the players.

0

As I said, "immediately before the loop". Don't edit your question with "problem solved" -- it's useful to archive the original.

0

Oh, I'm sorry i read your comment to fast, I thought you meant i never filled it correctly at all. Also, re-editing question.

2 answers

Learn to debug!

If something appears to do nothing, think of the reasons why that might be.

You were debugging by printing _G.realstats in the console -- but all sorts of things could happen between there and the code you think is running (in particular, the code you think is running could not be happening at all)

This is also an excellent argument against using _G. You should use functions or module scripts to fill values, not just hope someone else has plopped them in. Explicit communication is much better.

print(#_G.realstats) immediately before it's use guarantees you're seeing the right thing, and then you can begin to investigate why it might be that you get 0.

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