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

[Solved] giving gold for all players using table?

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

3 comments

0

So your players table is {player1, Player2} right?

0

yes

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.

1 answer

✓ Accepted answer

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.

1 comment

0

I found a complicated way to fix that issue but you function work as well. Thank you!

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