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

Why does this not change my table? When I changed it in for loop

Asked by 5 years ago
Edited by TheeDeathCaster 5 years ago
01local TableToMatchAccordingToIndex = {Cash = 0, Money = 0, ChaChing = 0}
02local PlayersData = {Cash = 123566, Money = 2345, ChaChing = 1231213}
03 
04 
05for i,v in pairs(TableToMatchAccordingToIndex) do
06 
07    for k,d in pairs(PlayersData) do
08 
09        if i == k then
10 
11            v = d
12 
13        end
14 
15    end
View all 22 lines...
0
most of the part is messed up... i doesn't equals k at the first place, and I don't get what you were trynna do with the v = d part TopBagon 109 — 5y
0
Please keep your variables appropriate; there's children on this site too. TheeDeathCaster 2368 — 5y

2 answers

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

The problem is because you're changing the v variable when you want to overwrite the index. This is the equivalent.

1local Val = script.Parent.StringValue.Value;
2Val = 'Hello World!';

It's changing the Val variable, but not the actual string value's. The solution to the problem's to overwrite the index in the table. For example, lets have 3 values in a table.

1local Dictionary = {Hello = 'no', Beautiful = 'u', World = 'hecker'};

To overwrite the index, you simply set the index to a different value.

1local Dictionary = {Hello = 'no', Beautiful = 'u', World = 'hecker'};
2Dictionary.World = 'nerd'; -- Overwrites `World` from 'hecker' to 'nerd'
3        -- Note that that's one way to index a table.

Please note that when there's no index prior in the table, it's going to set that new index.

So, in your case, you want to overwrite the indexes to the new values.

01local TableToMatchAccordingToIndex = {Cash = 0, Money = 0, ChaChing = 0};
02local PlayersData = {Cash = 123566, Money = 2345, ChaChing = 1231213};
03 
04 
05for Index, Value in pairs(PlayersData) do
06                                -- You'll notice the use of square brackets; they're used to index the table, especially in cases like this.
07    TableToMatchAccordingToIndex[Index] = Value; -- Overwrites the index to the `PlayersData`'s value index
08end
09 
10 
11for Index, Value in pairs(TableToMatchAccordingToIndex) do
12    print(Index, Value); -- Cash 123566 Money 2345 ChaChing 1231213
13end

Any questions? Feel free to ask. :) Hope this helped! :D

Ad
Log in to vote
1
Answered by 5 years ago
Edited by TheeDeathCaster 5 years ago

While this script is extremely questionable I have been able to fix your problem. The thing about tables is that if you want to change the value in a table then you have to do:

01local TableToChange = {V1 = 0,V2 = 0}
02local TableChanges = {V1 = 10,V2 = 25}
03 
04for i,v in pairs(TableToChange) do
05    for k,d in pairs(TableChanges) do
06        if i == k then
07            TableToChange[i] = TableChanges[k] --changes the value that is in the position I
08        end
09    end
10end
11 
12for i,v in pairs(TableToChange) do
13    print(i,v)
14end

Be mindful of the fact that Roblox does not like porn games and so I recommend changing pazzah to cat if you know whats good for you

0
Changed 1 word; please be mindful that there's kids on this site too. TheeDeathCaster 2368 — 5y

Answer this question