Still have questions? Join our Discord server and get real time help.
0

# [Solved] giving gold for all players using table?

Edited 3 months 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 3857 — 3mo
0
yes iagometroid 6 — 3mo
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 6 — 3mo

0

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 6 — 3mo