Trying to increment stat for all players that are in folder

I got this in a regular ServerScript to try and add 1 to their RoundsPlayed in Datastore2, but I keep getting the else statement.

If I remove the else part in the script I get nothing in the console.

V Script V

for i, v in pairs(game.Workspace.CurrentlyPlaying:GetChildren()) do
		local thatPlayer = game:GetService("Players"):FindFirstChild(v)
		if thatPlayer  then
			print(v.Name.." gains a round")
			local RoundsPlayed = DataStore2("Rounds", v)
			RoundsPlayed:Increment(1)
		else
			print("Couldn't find thatPlayer, (v) = "..tostring(v))
		end
	end

The reason this doesn’t work is because you are only passing “v” through the :FindFirstChild() parameter, which is not a string. I’m assuming that this folder contains the characters of the players. If so, you can use :GetPlayerFromCharacter() and pass the character instance. This should return the player if they are in game and your code should work like normal!

TLDR:

Switch this:

local thatPlayer = game:GetService("Players"):FindFirstChild(v)

to this:

local thatPlayer = game:GetService("Players"):GetPlayerFromCharacter(v)

Uhhhhhhh

Console output

UserId is not a valid member of Model “Workspace.CurrentlyPlaying.eggspIicit” - Server - DataStore2:530
Script ‘ServerScriptService.DataStore2’, Line 530 - function __call - Studio - DataStore2:530
Script ‘ServerScriptService.DataStore2’, Line 491 - function __call - Studio - DataStore2:491
Script ‘ServerScriptService.Main’, Line 88 - Studio - Main:88

You’re trying to get a property of the Player out of the Character.
Replace v in local RoundsPlayed = DataStore2("Rounds", v) with thatPlayer. This was a problem as a result of your code, and not the fix you were given.

2 Likes

you happen to be correct, thank you

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.