Player data isn't always loading

Hello,
I am currently working on a game and implemented datastore2 for saving player data and loading it. The issue I am having is that only about 1/4th of the time does it actually work for some odd reason. What I mean is that it’s not creating the variables or the folder for the variables.

local DataStore2 = require(1936396537)
local defaultWins = 0

game.Players.PlayerAdded:Connect(function(player)
	
	local WinsDataStore = DataStore2("WinsDataStore", player)
	local PointsDataStore = DataStore2("PointsDataStore", player)
	
	local varFolder = Instance.new("Folder", player)
	varFolder.Name = "vars"
	
	local playerAlive = Instance.new("IntValue", varFolder)
	playerAlive.Name = "playerAlive"
	playerAlive.Value = 0
	
	
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local wins = Instance.new("IntValue", leaderstats)
	wins.Name = "Wins"
	
	local points = Instance.new("IntValue", leaderstats)
	points.Name = "Points"
	
	local function winsUpdate(value)
		wins.Value = WinsDataStore:Get(value)
	end
	
	winsUpdate(defaultWins)
	
	WinsDataStore:OnUpdate(winsUpdate)
	
	local function pointsUpdate(value)
		points.Value = PointsDataStore:Get(value)
	end
	
	pointsUpdate(0)
	WinsDataStore:OnUpdate(pointsUpdate)
	
end)

What would be the best solution? Is there a way to validate that all of the data has been loaded?

PlayerAdded kinda sometimes is a bit dumb. @sleitnick has an excellent video on this that i’d recommend you watch.

Cheers! Thank you. I feel kind of dumb for not realizing this.

Ah, It’s not that you’re dumb. It’s more of roblox being stupid. I just happened to come across the video awhile back.

This seems irrelevant, I tested the script with a print command inside the first line of the function connected to the “PlayerAdded” event and it always fired.

local DataStore2 = require(1936396537)
local defaultWins = 0
local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	local WinsDataStore = DataStore2("WinsDataStore")
	WinsDataStore.Parent = player
	local PointsDataStore = DataStore2("PointsDataStore")
	PointsDataStore.Parent = player
	
	local varFolder = Instance.new("Folder")
	varFolder.Parent = player
	varFolder.Name = "vars"

	local playerAlive = Instance.new("IntValue")
	playerAlive.Parent = varFolder
	playerAlive.Name = "playerAlive"
	playerAlive.Value = 0


	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = player
	leaderstats.Name = "leaderstats"

	local wins = Instance.new("IntValue")
	wins.Parent = leaderstats
	wins.Name = "Wins"

	local points = Instance.new("IntValue")
	points.Parent = leaderstats
	points.Name = "Points"

	local function winsUpdate(value)
		wins.Value = WinsDataStore:Get(value)
	end

	winsUpdate(defaultWins)

	WinsDataStore:OnUpdate(winsUpdate)

	local function pointsUpdate(value)
		points.Value = PointsDataStore:Get(value)
	end

	pointsUpdate(0)
	WinsDataStore:OnUpdate(pointsUpdate)
end)

Your issue was that you were using the “Parent” parameter of “Instance.new()”, it’s strongly advised that you shouldn’t use it & instead opt to set the “Parent” property explicitly as I did.

Thank you for that information. I will definitely be changing that. Cheers!