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
local TableToMatchAccordingToIndex = {Cash = 0, Money = 0, ChaChing = 0}
local PlayersData = {Cash = 123566, Money = 2345, ChaChing = 1231213}


for i,v in pairs(TableToMatchAccordingToIndex) do

    for k,d in pairs(PlayersData) do

        if i == k then

            v = d

        end

    end

end


for i,v in pairs(TableToMatchAccordingToIndex) do -- I wanted this to print Cash = 1213566 Money 2345 and ChaChing 1231213
    print(i,v)
end
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.

local Val = script.Parent.StringValue.Value;
Val = '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.

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

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

local Dictionary = {Hello = 'no', Beautiful = 'u', World = 'hecker'};
Dictionary.World = 'nerd'; -- Overwrites `World` from 'hecker' to 'nerd'
        -- 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.

local TableToMatchAccordingToIndex = {Cash = 0, Money = 0, ChaChing = 0};
local PlayersData = {Cash = 123566, Money = 2345, ChaChing = 1231213};


for Index, Value in pairs(PlayersData) do
                                -- You'll notice the use of square brackets; they're used to index the table, especially in cases like this.
    TableToMatchAccordingToIndex[Index] = Value; -- Overwrites the index to the `PlayersData`'s value index
end


for Index, Value in pairs(TableToMatchAccordingToIndex) do
    print(Index, Value); -- Cash 123566 Money 2345 ChaChing 1231213
end

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:

local TableToChange = {V1 = 0,V2 = 0}
local TableChanges = {V1 = 10,V2 = 25}

for i,v in pairs(TableToChange) do
    for k,d in pairs(TableChanges) do
        if i == k then
            TableToChange[i] = TableChanges[k] --changes the value that is in the position I
        end
    end
end

for i,v in pairs(TableToChange) do
    print(i,v)
end

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