PlayerAdded doesn't fire

I’m not sure what to do here. I’m trying to create a “wins” datastore. I’ve did this once in a test project, and it works there, but it does not work here, even if I directly copy over the code from the test game. The print(“player added”) part doesn’t even run

local DataStoreService = game:GetService("DataStoreService")
local WinsAndStuff = DataStoreService:GetDataStore("WinsAndStuff")

game.Players.PlayerAdded:Connect(function(player)
	print("player added")
	local leaderstats = player.leaderstats
	
	local Wins = Instance.new("IntValue")
	print("Valued")
	Wins.Name = "Wins"
	print("Named")
	Wins.Parent = leaderstats
	print("Adopted")
	print("Does this part run?")
	
	local playerUserID = "Player_"..player.UserId
	
	-- Loading data
	local data
	local success, errormessage = pcall(function()
		if data then
			Wins.Value = data.Wins
		end
	end)
	game.ReplicatedStorage.InRound.Changed:Connect(function()
		local data = player.leaderstats.Wins.Value
		local success, errormessage = pcall(function()
			WinsAndStuff:SetAsync(playerUserID, data)
		end)
		
		if success then
			print("Successfully saved to "..playerUserID)
		else
			print("Error saving")
			warn(errormessage)
		end
	end)
end)
game.Players.PlayerRemoving:Connect(function(player)
	local data = player.leaderstats.Wins.Value
	local playerUserID = "Player_"..player.UserId
	local success, errormessage = pcall(function()
		WinsAndStuff:SetAsync(playerUserID, data)
	end)

	if success then
		print("Successfully saved to "..playerUserID)
	else
		print("Error saving")
		warn(errormessage)
	end
end)
1 Like

Is this in a server or local script?

1 Like

What if you use the correct format for the command:

game:GetService("Players"):Connect(function(player)
1 Like

game:GetService(“Players”):Connect???

1 Like

Heh. Duly edited to included the PlayerAdded event.

1 Like

This is a server script. It wouldn’t have worked in the other game if it was a localscript

1 Like

Doesn’t work (and yes I added the PlayerAdded)

1 Like

Ah, you can tell I didn’t get very far down the script before I stopped :-). You need to create the leaderstats folder in the same way you create the values so:

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

leaderstats already exists within another script

1 Like

ok. So it might not exist before this script event starts, so you could change it to wait for the folder to be added from the other script:

local leaderstats = player:WaitForChild("leaderstats")

That will make it wait for a max of 8 seconds I believe, which should be plenty of time for the other script to create the folder if it is initiated via the same event.

1 Like

The problem is that the print statement ABOVE that line doesn’t run

1 Like

Check if you have studio api access off. If it’s off, the script will error and never run the code. Go to file → game settings → security and turn it on and it should work properly.

1 Like

Just making sure you didn’t accidentally shove the script into ServerStorage instead of ServerScriptService like I keep doing heh

2 Likes

studio api access doesnt affect playeradded
the print statement would still run, but the datastore would return an error
In this case, the print statement didnt run, signifying that playeradded was never triggered

1 Like

there is a datastore getting function above there though, so that could be halting the thread

1 Like

@SomieStuff could you show us all the errors you have been getting from that script?
You can get them by going to view - output
turn that on and paste what you see there (minus your personal stuff) into here

1 Like

It does because if it is not on then the data store will error and cause the player added event to never be called.

Also you mentioned it in the next post so idk what you mean :skull:

1 Like

Just a quick note: it waits for 5 seconds. If the child doesn’t appear in that time, it prints a warning, but it will never stop waiting. You can optionally specify a timeout, when it will return nil:

foo:WaitForChild("Bar", 10)
--                      ^^ will return `nil` OR the child if it's
--                                added in the next 10 sec

By extension, you can disable the warning by adding an unusually high number, such as Infinity:

foo:WaitForChild("Bar", math.huge)

I always assume PlayerAdded may not fire on the first join, because the script might not connect the event before a player is in the game. Make the function you’re connecting to PlayerAdded an actual local function variable, and call that function on all existing players before you :Connect() that signal.

This turned out to be the exact issue. I’m not sure how that happened lol.

1 Like