How to send increment to all players datastore2

trying to make this send to all players

local ParkourBuxStore = DataStore2("Rounds", game.Players:GetUserIdFromNameAsync())
	ParkourBuxStore:Increment(1)

error

Argument 1 missing or nil

Try using

Players:GetPlayers()
local ParkourBuxStore = DataStore2("Rounds", game.Players:GetPlayers())
	ParkourBuxStore:Increment(1)

error

 ServerScriptService.DataStore2:479: DataStore2() API call expected {string dataStoreName, Instance player}, got {string, table}

I don’t have any idea of what you should do sorry, I’m not good with DataStores

1 Like

This function simply returns the Player.UserId of a user whose name is specified. You should be using the specific player not the ID.
local ParkourBuxStore = DataStore2("Rounds", Player)
You will have to make a Player variable that connects to whatever player you want. Normally you would do this either with a RemoteEvent or inside PlayerAdded:

game.Players.PlayerAdded:Connect(function(Player)
	local ParkourBuxStore = DataStore2("Rounds", Player)
end)

Its in mid game, not when they join ? how would i do it that way (without PlayerAdded)

Use a for-in-pair loop to loop through all the players.

1 Like

this is what i tried

for i, v in pairs(game.Players:GetPlayers()) do
		local RoundsPlayed = DataStore2("Rounds", v.UserId)
		RoundsPlayed:Increment(1)
	end

got this error

ServerScriptService.DataStore2:479: DataStore2() API call expected {string dataStoreName, Instance player}, got {string, number} 

then i tried this

for i, v in pairs(game.Players:GetPlayers()) do
		local RoundsPlayed = DataStore2("Rounds", v)
		RoundsPlayed:Increment(1)
	end

and got this

ServerScriptService.DataStore2:191: attempt to perform arithmetic (add) on nil and number 

The error is as stated, you’re using a number instead of the player.

i tried that with the v instread of .UserId

Unfortunately I never used DataStore2 for my datastore, but I’m assuming that the error is produced because there is no data to be found under that player.

DataStore2 should be used like this:

local RoundsStore = DataStore2("Rounds", player)
	RoundsStore:Increment(1)

I think he knows about that, but he wants the data to increment for all players

Yeah like you said from that error it’s saying the data doesn’t exist. His code should be working fine if everything is defined correctly. I would try testing DataStore2s in a real server not in studio it’s a bit buggy for me.
@eggspIicit if you haven’t set the data as a number before hand it’s not going to find any number to increment.
Try using RoundsPlayed:Increment(1,0) this should set their data to 0 if it doesn’t exist and then it will add 1. Read the API documentation for more details on setting data.

You need to set the default parameter for the data if it does not exist. Additionally, you should be using Combine.

1 Like

I already have set up all of the datastore2 configurations, they start with 0 Rounds.

local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local Workspace = game:GetService("Workspace")

local DataStore2 = require(ServerScriptService.DataStore2)

DataStore2.Combine("DATA", "Wins","PB$", "Rounds")

Players.PlayerAdded:Connect(function(player)
	local RoundsPlayed = DataStore2("Rounds", player)

	local hidenstats = Instance.new("Folder")
	hidenstats.Name = "hidenstats"
	
	local Rounds = Instance.new("NumberValue")
	Rounds.Name = "Rounds"
	Rounds.Value = ParkourBuxStore:Get(0)
	Rounds.Parent = hidenstats

	RoundsPlayed:OnUpdate(function(delta)
		Rounds.Value = delta
	end)
	

	hidenstats.Parent = player
end)

*bump need help still

It should be:

Rounds.Value += delta

Because delta is the change from the last value.

Try this instead

local RoundsStore = DataStore2("Rounds", player)
RoundsStore:Update(function(prev)
	prev = prev or 0
	return prev + 1
end)