UserId is not a valid member of Players - DataStore script

Hi everyone,

I’ve been trying to make A VERY SIMPLE game (although it ain’t simple at all for me lol). Anyways, I have been having issues. Let me tell you why.

image
This is the error I keep getting.

local DataStoreService = game:GetService("DataStoreService")
local player = game:GetService("Players")
local numberofwins = 0
local wStore = DataStoreService:GetDataStore("Wins")
local success, errorMessage =
	pcall(function()
	wStore:SetAsync(player.UserId.."Wins", numberofwins)
end)
if not success then
	print(errorMessage)
end

This is the code. I’m trying to make my ‘first’ datastore and I’m struggling to do so. Any help, please? ^-^

Thanks,
Aki

this is wrong
first, if you want to save data for the players then
you need those events
https://developer.roblox.com/en-us/api-reference/class/Players

game.Players.PlayerAdded(thePlayerThatWasAdded)
game.Players.PlayerRemoving(thePlayerIsGone)

https://developer.roblox.com/en-us/api-reference/event/Players/PlayerAdded
https://developer.roblox.com/en-us/api-reference/event/Players/PlayerRemoving
and DataStoreService

the players Service does not contain UserId, its players do

1 Like

player is set to the player service not a player

1 Like

You should put “player” as a parameter on PlayerAdded event, something like this:

image

because your “player” variable is actually not the player, it is the Players Service

image

And it should be :

wStore:GetASync()

because you want the player to load their saved data when they are joining your game, if you put wStore:SetAsync() you will be saving the data instead of loading it.

and when you want to save the data It’s when the player is leaving the game:

game.Players.PlayerRemoving:Connect(function(player)
wStore:SetAsync()
end)

and for preventing data loss just write this:

game:BindToClose:Connect(function()
for i, player in pairs(game.Players:GetPlayers()) do
if player then
player:Kick()
end
end
wait(3)
end)

Just remember “:SetAsync()” = Saving the data and “:GetAsync()” = loading the data.

Also I recommend you to watch alvinbloxx videos because they will teach you a lot of important things :raised_hand_with_fingers_splayed: :grin:

1 Like
local DataStoreService = game:GetService("DataStoreService")
local players = game:GetService("Players")
local player = players.LocalPlayer
local numberofwins = 0
local wStore = DataStoreService:GetDataStore("Wins")
local success, errorMessage =
	pcall(function()
	wStore:SetAsync(player.UserId.."Wins", numberofwins)
end)
if not success then
	print(errorMessage)
end

I’ve made the slight modification for you which will grab the correct user ID of the local player.

Datastore:SetAsync() is a server-side function, so you cannot use this in a LocalScript.

I just copied and pasted the original code and only made the outlined modification, he will need to tweak it further to work for his game.

Well, I was saying this because you used Players.LocalPlayer, which can only be used in LocalScripts. Perhaps try to get the player instance when the player joins/leaves.