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.
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? ^-^
You should put “player” as a parameter on PlayerAdded event, something like this:
because your “player” variable is actually not the player, it is the Players Service
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:
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.
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.