--Fires when ther player joins the game.

game.Players.PlayerAdded:connect(function(Player)

	--Wait for everything to load.	
	repeat
		game.Players:WaitForChild(Player.Name)
	until game.Players:FindFirstChild(Player.Name)
	repeat
		Player:WaitForChild("Backpack")
	until Player:FindFirstChild("Backpack")
	
	--Load the items from the datastore.
	LoadData(Player)
	
	
	--Connect the functions to various other events.
	
	--Load the items when the player respawns.
	Player.CharacterAdded:connect(function()
		LoadData(Player)
	end)	
	
	--Save the items when the player receives a new item.
	game.Players.PlayerRemoving:connect(function(Player)
		SaveData(Player)
	end)
	
	--Save the items when the player leaves the game.
	Player.Backpack.ChildAdded:connect(function()
		SaveData(Player)
	end)
	
end)
