Variable stops PlayerAdded event?

Hey DevForum, I have come across a problem and I’m pretty much just confused on what’s happening.

Whenever I put this variable in my code: local DataStore2 = require(1936396537)
It stops a PlayerAdded Event.

Code:

local DataStore2 = require(1936396537)

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

local function PlayerJoined(player)
	
	print("someone has joined")

end

game.Players.PlayerAdded:Connect(PlayerJoined)

Have in mind this is not the full code.
No errors show up in output, I’m pretty much just clueless.

This is also my first time using DataStore2, sorry if that’s the error.

1 Like

The line of code is yielding the script, thus player may join before the connection is set up, that is why the best way to handle a player joining is to also loop over all the current players in the server to handle edge cases:

local DataStore2 = require(1936396537)

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

local function PlayerJoined(player)
	
	print("someone has joined")

end

game.Players.PlayerAdded:Connect(PlayerJoined)
for _, Player in ipairs(game.Players:GetPlayers()) do
     PlayerJoined(Player)
end
2 Likes

Sorry but, I do not quite understand what’s going on the script, Could you explain it a little bit more?

It is checking if any players are in the server after setting up the PlayerAdded event is set up, since requiring a module by Id will yield the script, thus, a player may join the game before the PlayerAdded event connection set up, and so it will not detect when that player joins the server.

1 Like

Ohh okay I understand now, I thought it did something else, first time working with modules too :grimacing: thanks!

2 Likes