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

[Solved] giving gold for all players using table?

Asked by 6 years ago
Edited 6 years ago

I put all the names that gonna get the gold in a table, but inst a String, it is an ObjectValue.

local gold = 25
-- 'players' is the table within 2 Object Values.
for _,v in pairs(players) do
    v.Value.Stats.Gold.Value = v.Value.Stats.Gold.Value + gold
    end

when I send gold to the players, for example, player1, and player2 the condition gives 2x gold for player 1 and 1x gold to player 2. Why?

The same thing happens here:

local gold = 25
for b = 1,#players do
    players[b].Value.Stats.Gold.Value = players[b].Value.Stats.Gold.Value + gold
    end
0
So your players table is {player1, Player2} right? Shawnyg 4330 — 6y
0
yes iagometroid 61 — 6y
0
the problem is if i put more players in the table (player1,player2,player3,player4) the first one get 4x gold, the second 3x, etc. I want everyone get 25 gold. iagometroid 61 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

Create a function that will do this for you automatically. Call the function whenever you want to give all players x gold. Check out the code below.

local Players = game:GetService("Players");

function giveGold (goldAmount)

    for _, player in pairs(Players) do
        local gold = player.Value.Status.Gold;
        gold.Value = gold.Value + goldAmount;
    end

end

Then simply call the function with giveGold(25) and it should work as a charm.

0
I found a complicated way to fix that issue but you function work as well. Thank you! iagometroid 61 — 6y
Ad

Answer this question