game.Players.PlayerAdded event is not working

I used DataStore2 to save a StringValue, but the thing is that the game doesn’t implement a leaderstats folder or a StringValue into the player’s folder. How do I fix that?

DataStore2.Combine("MasterKey","PersonalityTrait")

game.Players.PlayerAdded:Connect(function(player)
	local dataTrait = DataStore2("PersonalityTrait", player)
	
	local leaderstats = Instance.new("Folder", player); leaderstats.Name = "leaderstats"
	local PersonalityTraitValue = Instance.new("StringValue", leaderstats);PersonalityTraitValue.Name = "PersonalityTrait"
	    
	if dataTrait:Get() == nil then
		PersonalityTraitValue.Value = dataTrait:Get()
	else
		PersonalityTraitValue.Value = ""
	end
	
	PersonalityTraitValue.Changed:Connect(function()
		dataTrait:Set(PersonalityTraitValue.Value)
	end)
end)

Are you sure PlayerAdded is not firing?
Try creating the leaderboard before setting dataTrait.

Add a print right at the beginning of the function, it will work for sure.

2 Likes

Is this currently happening in your Studio? If so, tell me to send you a link to a post which answers the question.

1 Like

Yeah, it doesn’t print. I moved dataTrait after making the leaderboard but it still doesn’t print in the function.

That means the script never gets to fire that piece of code. Possibly due to
Datastore2.Combine("MasterKey", "PersonalityTrait")
yielding for too long, or erroring.

2 Likes

So should I move the .Combine to somewhere else or should I just remove it? I’m confused.

In case the error is occurring in the Studio here is a link:

1 Like

I am not familiar with Datastore2. If you connect a function to PlayerAdded event on top of a script, it will run with no trouble as long as the script is enabled.

Your Datastore2.Combine() line might be messing around with the code. It could be endlessly yielding, looping or it errored and stopped the script from running?

2 Likes